Bug 672439 - Avoid duplicate names for system compartment reporters. r=gal.
authorNicholas Nethercote <nnethercote@mozilla.com>
Tue, 19 Jul 2011 16:59:16 -0700
changeset 73828 877914fc95bb20f2e98b0d69ad9964af70f9ac83
parent 73827 adc8f12b54dfbd7287447bcaae3dd81c76a1f3a2
child 73829 1bee4ff7c91c5d0eb3af554b95c230a396cebe62
push idunknown
push userunknown
push dateunknown
reviewersgal
bugs672439
milestone8.0a1
Bug 672439 - Avoid duplicate names for system compartment reporters. r=gal.
caps/src/nsSystemPrincipal.cpp
js/src/xpconnect/src/xpcjsruntime.cpp
--- a/caps/src/nsSystemPrincipal.cpp
+++ b/caps/src/nsSystemPrincipal.cpp
@@ -81,16 +81,18 @@ nsSystemPrincipal::Release()
   return count;
 }
 
 
 ///////////////////////////////////////
 // Methods implementing nsIPrincipal //
 ///////////////////////////////////////
 
+#define SYSTEM_PRINCIPAL_SPEC "[System Principal]"
+
 NS_IMETHODIMP
 nsSystemPrincipal::GetPreferences(char** aPrefName, char** aID,
                                   char** aSubjectName,
                                   char** aGrantedList, char** aDeniedList,
                                   PRBool* aIsTrusted)
 {
     // The system principal should never be streamed out
     *aPrefName = nsnull;
@@ -190,17 +192,17 @@ nsSystemPrincipal::GetURI(nsIURI** aURI)
 {
     *aURI = nsnull;
     return NS_OK;
 }
 
 NS_IMETHODIMP 
 nsSystemPrincipal::GetOrigin(char** aOrigin)
 {
-    *aOrigin = ToNewCString(NS_LITERAL_CSTRING("[System Principal]"));
+    *aOrigin = ToNewCString(NS_LITERAL_CSTRING(SYSTEM_PRINCIPAL_SPEC));
     return *aOrigin ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
 }
 
 NS_IMETHODIMP 
 nsSystemPrincipal::GetFingerprint(nsACString& aID)
 {
     return NS_ERROR_NOT_AVAILABLE;
 }
@@ -303,21 +305,16 @@ nsSystemPrincipal::Write(nsIObjectOutput
 /////////////////////////////////////////////
 // Constructor, Destructor, initialization //
 /////////////////////////////////////////////
 
 nsSystemPrincipal::nsSystemPrincipal()
 {
 }
 
-// Don't rename the system principal!
-// The JS engine (NewCompartment) relies on this name. 
-// XXX: bug 669123 will fix this hack.
-#define SYSTEM_PRINCIPAL_SPEC "[System Principal]"
-
 nsresult
 nsSystemPrincipal::Init(JSPrincipals **jsprin)
 {
     // Use an nsCString so we only do the allocation once here and then
     // share with nsJSPrincipals
     nsCString str(SYSTEM_PRINCIPAL_SPEC);
     if (!str.EqualsLiteral(SYSTEM_PRINCIPAL_SPEC)) {
         NS_WARNING("Out of memory initializing system principal");
--- a/js/src/xpconnect/src/xpcjsruntime.cpp
+++ b/js/src/xpconnect/src/xpcjsruntime.cpp
@@ -41,16 +41,17 @@
 /* Per JSRuntime object */
 
 #include "xpcprivate.h"
 #include "WrapperFactory.h"
 #include "dom_quickstubs.h"
 
 #include "jsgcchunk.h"
 #include "nsIMemoryReporter.h"
+#include "nsPrintfCString.h"
 #include "mozilla/FunctionTimer.h"
 #include "prsystem.h"
 
 #ifdef MOZ_CRASHREPORTER
 #include "nsExceptionHandler.h"
 #endif
 
 using namespace mozilla;
@@ -1316,16 +1317,24 @@ private:
                     name.Assign(c->principals->codebase);
                     char* cur = name.BeginWriting();
                     char* end = name.EndWriting();
                     for (; cur < end; ++cur) {
                         if ('/' == *cur) {
                             *cur = '\\';
                         }
                     }
+                    // If it's the system compartment, append the address.
+                    // This means that multiple system compartments (and there
+                    // can be many) can be distinguished.
+                    if (c->isSystemCompartment) {
+                        static const int maxLength = 31;   // ample; 64-bit address max is 18 chars
+                        nsPrintfCString address(maxLength, ", 0x%llx", PRUint64(c));
+                        name.Append(address);
+                    }
                 } else {
                     name = NS_LITERAL_CSTRING("null-codebase");
                 }
             } else {
                 name = NS_LITERAL_CSTRING("null-principal");
             }
         }