From c93fca087ecd15439c21232c68c8ff6164023197 Mon Sep 17 00:00:00 2001 From: Michael Layzell Date: Tue, 11 Jul 2017 16:52:09 -0400 Subject: [PATCH] Bug 1380096 - Don't require that sMainThreadRunnableName is null-terminated, r=erahm MozReview-Commit-ID: 9Zo1vjmKzZC --- xpcom/threads/ThreadStackHelper.cpp | 13 +++++++------ xpcom/threads/nsThread.cpp | 8 +++++--- xpcom/threads/nsThread.h | 2 +- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/xpcom/threads/ThreadStackHelper.cpp b/xpcom/threads/ThreadStackHelper.cpp index cf4e7843ef59..933dafb5bb84 100644 --- a/xpcom/threads/ThreadStackHelper.cpp +++ b/xpcom/threads/ThreadStackHelper.cpp @@ -138,7 +138,7 @@ ThreadStackHelper::GetStacksInternal(Stack* aStack, ScopedSetPtr nativeStackPtr(mNativeStackToFill, aNativeStack); #endif - char nameBuffer[1000] = {0}; + Vector nameBuffer; auto callback = [&, this] (void** aPCs, size_t aCount, bool aIsMainThread) { // NOTE: We cannot allocate any memory in this callback, as the target // thread is suspended, so we first copy it into a stack-allocated buffer, @@ -149,9 +149,10 @@ ThreadStackHelper::GetStacksInternal(Stack* aStack, // main thread, so we only want to read sMainThreadRunnableName and copy its // value in the case that we are currently suspending the main thread. if (aIsMainThread && nsThread::sMainThreadRunnableName) { - strncpy(nameBuffer, nsThread::sMainThreadRunnableName, sizeof(nameBuffer)); - // Make sure the string is null-terminated. - nameBuffer[sizeof(nameBuffer) - 1] = '\0'; + MOZ_ALWAYS_TRUE( + nameBuffer.append(nsThread::sMainThreadRunnableName->BeginReading(), + std::min(uint32_t(nameBuffer.sMaxInlineStorage), + uint32_t(nsThread::sMainThreadRunnableName->Length())))); } #ifdef MOZ_THREADSTACKHELPER_PSEUDO @@ -177,8 +178,8 @@ ThreadStackHelper::GetStacksInternal(Stack* aStack, } // Copy the name buffer allocation into the output string. - if (nameBuffer[0] != 0) { - aRunnableName = nameBuffer; + if (nameBuffer.length() > 0) { + aRunnableName.AssignASCII(nameBuffer.begin(), nameBuffer.length()); } #endif } diff --git a/xpcom/threads/nsThread.cpp b/xpcom/threads/nsThread.cpp index 202025c5ca35..cb74b9a62299 100644 --- a/xpcom/threads/nsThread.cpp +++ b/xpcom/threads/nsThread.cpp @@ -99,7 +99,7 @@ static LazyLogModule sThreadLog("nsThread"); NS_DECL_CI_INTERFACE_GETTER(nsThread) -const char* nsThread::sMainThreadRunnableName = nullptr; +const nsACString* nsThread::sMainThreadRunnableName = nullptr; //----------------------------------------------------------------------------- // Because we do not have our own nsIFactory, we have to implement nsIClassInfo @@ -1422,15 +1422,17 @@ nsThread::ProcessNextEvent(bool aMayWait, bool* aResult) // If we're on the main thread, we want to record our current runnable's // name in a static so that BHR can record it. - const char* restoreRunnableName = nullptr; + const nsACString* restoreRunnableName = nullptr; auto clear = MakeScopeExit([&] { if (MAIN_THREAD == mIsMainThread) { + MOZ_ASSERT(NS_IsMainThread()); sMainThreadRunnableName = restoreRunnableName; } }); if (MAIN_THREAD == mIsMainThread) { + MOZ_ASSERT(NS_IsMainThread()); restoreRunnableName = sMainThreadRunnableName; - sMainThreadRunnableName = name.get(); + sMainThreadRunnableName = &name; } #endif diff --git a/xpcom/threads/nsThread.h b/xpcom/threads/nsThread.h index 5bbe8d8d78a7..87ce954960b5 100644 --- a/xpcom/threads/nsThread.h +++ b/xpcom/threads/nsThread.h @@ -93,7 +93,7 @@ public: static bool SaveMemoryReportNearOOM(ShouldSaveMemoryReport aShouldSave); #endif - static const char* sMainThreadRunnableName; + static const nsACString* sMainThreadRunnableName; private: void DoMainThreadSpecificProcessing(bool aReallyWait);