Bug 1444361: Don't obtain cert subject info on main thread; r=chutten
authorAaron Klotz <aklotz@mozilla.com>
Fri, 09 Mar 2018 11:27:51 -0700
changeset 462440 017dbfcb23698b9484a4467221e86a0dca980326
parent 462439 1caceba533ebc4ecf677b434299e37e3511356dd
child 462441 cb808626c8df4679b351e184c955d1d15cc2c2bf
push id1683
push usersfraser@mozilla.com
push dateThu, 26 Apr 2018 16:43:40 +0000
treeherdermozilla-release@5af6cb21869d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerschutten
bugs1444361
milestone60.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1444361: Don't obtain cert subject info on main thread; r=chutten
toolkit/components/telemetry/Telemetry.cpp
toolkit/components/telemetry/TelemetryCommon.cpp
toolkit/components/telemetry/TelemetryCommon.h
--- a/toolkit/components/telemetry/Telemetry.cpp
+++ b/toolkit/components/telemetry/Telemetry.cpp
@@ -55,16 +55,17 @@
 #include "ipc/TelemetryIPCAccumulator.h"
 #include "TelemetryScalar.h"
 #include "TelemetryEvent.h"
 #include "WebrtcTelemetry.h"
 #include "nsTHashtable.h"
 #include "nsHashKeys.h"
 #include "nsBaseHashtable.h"
 #include "nsClassHashtable.h"
+#include "nsDataHashtable.h"
 #include "nsXULAppAPI.h"
 #include "nsReadableUtils.h"
 #include "nsThreadUtils.h"
 #if defined(XP_WIN)
 #include "nsUnicharUtils.h"
 #endif
 #include "nsNetCID.h"
 #include "nsNetUtil.h"
@@ -94,16 +95,17 @@
 #include "KeyedStackCapturer.h"
 #endif // MOZ_GECKO_PROFILER
 
 namespace {
 
 using namespace mozilla;
 using namespace mozilla::HangMonitor;
 using Telemetry::Common::AutoHashtable;
+using Telemetry::Common::ToJSString;
 using mozilla::dom::Promise;
 using mozilla::dom::AutoJSAPI;
 using mozilla::Telemetry::HangReports;
 using mozilla::Telemetry::CombinedStacks;
 using mozilla::Telemetry::ComputeAnnotationsKey;
 using mozilla::Telemetry::TelemetryIOInterposeObserver;
 
 #if defined(MOZ_GECKO_PROFILER)
@@ -786,26 +788,32 @@ TelemetryImpl::SnapshotCapturedStacks(bo
 }
 
 #if defined(MOZ_GECKO_PROFILER)
 class GetLoadedModulesResultRunnable final : public Runnable
 {
   nsMainThreadPtrHandle<Promise> mPromise;
   SharedLibraryInfo mRawModules;
   nsCOMPtr<nsIThread> mWorkerThread;
+#if defined(XP_WIN)
+  nsDataHashtable<nsStringHashKey, nsString> mCertSubjects;
+#endif // defined(XP_WIN)
 
 public:
   GetLoadedModulesResultRunnable(const nsMainThreadPtrHandle<Promise>& aPromise,
                                  const SharedLibraryInfo& rawModules)
     : mozilla::Runnable("GetLoadedModulesResultRunnable")
     , mPromise(aPromise)
     , mRawModules(rawModules)
     , mWorkerThread(do_GetCurrentThread())
   {
     MOZ_ASSERT(!NS_IsMainThread());
+#if defined(XP_WIN)
+    ObtainCertSubjects();
+#endif // defined(XP_WIN)
   }
 
   NS_IMETHOD
   Run() override
   {
     MOZ_ASSERT(NS_IsMainThread());
 
     mWorkerThread->Shutdown();
@@ -895,22 +903,19 @@ public:
 
       if (!JS_DefineProperty(cx, moduleObj, "version", version, JSPROP_ENUMERATE)) {
         mPromise->MaybeReject(NS_ERROR_FAILURE);
         return NS_OK;
       }
 
 #if defined(XP_WIN)
       // Cert Subject.
-      // NB: Currently we cannot lower this down to the profiler layer due to
-      // differing startup dependencies between the profiler and DllServices.
-      RefPtr<DllServices> dllSvc(DllServices::Get());
-      auto orgName = dllSvc->GetBinaryOrgName(info.GetModulePath().get());
-      if (orgName) {
-        JS::RootedString jsOrg(cx, JS_NewUCStringCopyZ(cx, (char16_t*) orgName.get()));
+      nsString* subject = mCertSubjects.GetValue(info.GetModulePath());
+      if (subject) {
+        JS::RootedString jsOrg(cx, ToJSString(cx, *subject));
         if (!jsOrg) {
           mPromise->MaybeReject(NS_ERROR_FAILURE);
           return NS_OK;
         }
 
         JS::RootedValue certSubject(cx);
         certSubject.setString(jsOrg);
 
@@ -926,16 +931,38 @@ public:
         mPromise->MaybeReject(NS_ERROR_FAILURE);
         return NS_OK;
       }
     }
 
     mPromise->MaybeResolve(moduleArray);
     return NS_OK;
   }
+
+private:
+#if defined(XP_WIN)
+  void ObtainCertSubjects()
+  {
+    MOZ_ASSERT(!NS_IsMainThread());
+
+    // NB: Currently we cannot lower this down to the profiler layer due to
+    // differing startup dependencies between the profiler and DllServices.
+    RefPtr<DllServices> dllSvc(DllServices::Get());
+
+    for (unsigned int i = 0, n = mRawModules.GetSize(); i != n; i++) {
+      const SharedLibrary& info = mRawModules.GetEntry(i);
+
+      auto orgName = dllSvc->GetBinaryOrgName(info.GetModulePath().get());
+      if (orgName) {
+        mCertSubjects.Put(info.GetModulePath(),
+                          nsDependentString(orgName.get()));
+      }
+    }
+  }
+#endif // defined(XP_WIN)
 };
 
 class GetLoadedModulesRunnable final : public Runnable
 {
   nsMainThreadPtrHandle<Promise> mPromise;
 
 public:
   explicit GetLoadedModulesRunnable(
--- a/toolkit/components/telemetry/TelemetryCommon.cpp
+++ b/toolkit/components/telemetry/TelemetryCommon.cpp
@@ -172,11 +172,17 @@ IsValidIdentifierString(const nsACString
 
 JSString*
 ToJSString(JSContext* cx, const nsACString& aStr)
 {
   const NS_ConvertUTF8toUTF16 wide(aStr);
   return JS_NewUCStringCopyN(cx, wide.Data(), wide.Length());
 }
 
+JSString*
+ToJSString(JSContext* cx, const nsAString& aStr)
+{
+  return JS_NewUCStringCopyN(cx, aStr.Data(), aStr.Length());
+}
+
 } // namespace Common
 } // namespace Telemetry
 } // namespace mozilla
--- a/toolkit/components/telemetry/TelemetryCommon.h
+++ b/toolkit/components/telemetry/TelemetryCommon.h
@@ -114,13 +114,23 @@ IsValidIdentifierString(const nsACString
  *
  * @param cx The JS context.
  * @param aStr The UTF8 string.
  * @returns a JavaScript string.
  */
 JSString*
 ToJSString(JSContext* cx, const nsACString& aStr);
 
+/**
+ * Convert the given UTF16 string to a JavaScript string.
+ *
+ * @param cx The JS context.
+ * @param aStr The UTF16 string.
+ * @returns a JavaScript string.
+ */
+JSString*
+ToJSString(JSContext* cx, const nsAString& aStr);
+
 } // namespace Common
 } // namespace Telemetry
 } // namespace mozilla
 
 #endif // TelemetryCommon_h__