Bug 1465728 part 2 - Remove the compartment name callback. r=luke

This commit is contained in:
Jan de Mooij 2018-06-06 11:44:17 +02:00
parent 71d86bded9
commit 3d013f2eef
8 changed files with 28 additions and 48 deletions

View File

@ -622,12 +622,6 @@ JS_SetSizeOfIncludingThisCompartmentCallback(JSContext* cx,
cx->runtime()->sizeOfIncludingThisCompartmentCallback = callback;
}
JS_PUBLIC_API(void)
JS_SetCompartmentNameCallback(JSContext* cx, JSCompartmentNameCallback callback)
{
cx->runtime()->compartmentNameCallback = callback;
}
#if defined(NIGHTLY_BUILD)
JS_PUBLIC_API(void)
JS_SetErrorInterceptorCallback(JSRuntime* rt, JSErrorInterceptor* callback)

View File

@ -963,9 +963,6 @@ extern JS_PUBLIC_API(void)
JS_SetSizeOfIncludingThisCompartmentCallback(JSContext* cx,
JSSizeOfIncludingThisCompartmentCallback callback);
extern JS_PUBLIC_API(void)
JS_SetCompartmentNameCallback(JSContext* cx, JSCompartmentNameCallback callback);
extern JS_PUBLIC_API(void)
JS_SetWrapObjectCallbacks(JSContext* cx, const JSWrapObjectCallbacks* callbacks);

View File

@ -556,13 +556,13 @@ LCovRealm::writeRealmName(JS::Realm* realm)
// thus we escape invalid chracters with a "_" symbol in front of its
// hexadecimal code.
outTN_.put("TN:");
if (cx->runtime()->compartmentNameCallback) {
if (cx->runtime()->realmNameCallback) {
char name[1024];
{
// Hazard analysis cannot tell that the callback does not GC.
JS::AutoSuppressGCAnalysis nogc;
JSCompartment* comp = JS::GetCompartmentForRealm(realm);
(*cx->runtime()->compartmentNameCallback)(cx, comp, name, sizeof(name));
Rooted<Realm*> rootedRealm(cx, realm);
(*cx->runtime()->realmNameCallback)(cx, rootedRealm, name, sizeof(name));
}
for (char *s = name; s < name + sizeof(name) && *s; s++) {
if (('a' <= *s && *s <= 'z') ||

View File

@ -107,7 +107,6 @@ JSRuntime::JSRuntime(JSRuntime* parentRuntime)
allowRelazificationForTesting(false),
destroyCompartmentCallback(nullptr),
sizeOfIncludingThisCompartmentCallback(nullptr),
compartmentNameCallback(nullptr),
destroyRealmCallback(nullptr),
realmNameCallback(nullptr),
externalStringSizeofCallback(nullptr),

View File

@ -354,9 +354,6 @@ struct JSRuntime : public js::MallocProvider<JSRuntime>
/* Compartment memory reporting callback. */
js::MainThreadData<JSSizeOfIncludingThisCompartmentCallback> sizeOfIncludingThisCompartmentCallback;
/* Call this to get the name of a compartment. */
js::MainThreadData<JSCompartmentNameCallback> compartmentNameCallback;
/* Realm destroy callback. */
js::MainThreadData<JS::DestroyRealmCallback> destroyRealmCallback;

View File

@ -1074,13 +1074,13 @@ XPCJSRuntime::~XPCJSRuntime()
MOZ_COUNT_DTOR_INHERITED(XPCJSRuntime, CycleCollectedJSRuntime);
}
// If |*anonymizeID| is non-zero and this is a user compartment, the name will
// If |*anonymizeID| is non-zero and this is a user realm, the name will
// be anonymized.
static void
GetCompartmentName(JSCompartment* c, nsCString& name, int* anonymizeID,
bool replaceSlashes)
GetRealmName(JS::Realm* realm, nsCString& name, int* anonymizeID,
bool replaceSlashes)
{
JS::Realm* realm = JS::GetRealmForCompartment(c);
JSCompartment* c = JS::GetCompartmentForRealm(realm);
if (*anonymizeID && !js::IsSystemCompartment(c)) {
name.AppendPrintf("<anonymized-%d>", *anonymizeID);
*anonymizeID += 1;
@ -1157,7 +1157,7 @@ GetCompartmentName(JSCompartment* c, nsCString& name, int* anonymizeID,
}
extern void
xpc::GetCurrentCompartmentName(JSContext* cx, nsCString& name)
xpc::GetCurrentRealmName(JSContext* cx, nsCString& name)
{
RootedObject global(cx, JS::CurrentGlobalOrNull(cx));
if (!global) {
@ -1165,9 +1165,9 @@ xpc::GetCurrentCompartmentName(JSContext* cx, nsCString& name)
return;
}
JSCompartment* compartment = GetObjectCompartment(global);
JS::Realm* realm = GetNonCCWObjectRealm(global);
int anonymizeID = 0;
GetCompartmentName(compartment, name, &anonymizeID, false);
GetRealmName(realm, name, &anonymizeID, false);
}
void
@ -2047,10 +2047,10 @@ class JSMainRuntimeRealmsReporter final : public nsIMemoryReporter
static void RealmCallback(JSContext* cx, void* vdata, Handle<Realm*> realm) {
// silently ignore OOM errors
JSCompartment* c = JS::GetCompartmentForRealm(realm);
Data* data = static_cast<Data*>(vdata);
nsCString path;
GetCompartmentName(c, path, &data->anonymizeID, /* replaceSlashes = */ true);
GetRealmName(realm, path, &data->anonymizeID, /* replaceSlashes = */ true);
JSCompartment* c = JS::GetCompartmentForRealm(realm);
path.Insert(js::IsSystemCompartment(c)
? NS_LITERAL_CSTRING("js-main-runtime-realms/system/")
: NS_LITERAL_CSTRING("js-main-runtime-realms/user/"),
@ -2195,10 +2195,9 @@ class XPCJSRuntimeStats : public JS::RuntimeStats
virtual void initExtraRealmStats(Handle<Realm*> realm,
JS::RealmStats* realmStats) override
{
JSCompartment* c = JS::GetCompartmentForRealm(realm);
xpc::RealmStatsExtras* extras = new xpc::RealmStatsExtras;
nsCString cName;
GetCompartmentName(c, cName, &mAnonymizeID, /* replaceSlashes = */ true);
nsCString rName;
GetRealmName(realm, rName, &mAnonymizeID, /* replaceSlashes = */ true);
// Get the realm's global.
AutoSafeJSContext cx;
@ -2228,10 +2227,12 @@ class XPCJSRuntimeStats : public JS::RuntimeStats
extras->domPathPrefix.AssignLiteral("explicit/dom/no-global?!/");
}
if (needZone)
if (needZone) {
JSCompartment* c = JS::GetCompartmentForRealm(realm);
extras->jsPathPrefix += nsPrintfCString("zone(0x%p)/", (void*)js::GetCompartmentZone(c));
}
extras->jsPathPrefix += NS_LITERAL_CSTRING("realm(") + cName + NS_LITERAL_CSTRING(")/");
extras->jsPathPrefix += NS_LITERAL_CSTRING("realm(") + rName + NS_LITERAL_CSTRING(")/");
// extras->jsPathPrefix is used for almost all the realm-specific
// reports. At this point it has the form
@ -2643,14 +2644,14 @@ SetUseCounterCallback(JSObject* obj, JSUseCounter counter)
}
static void
CompartmentNameCallback(JSContext* cx, JSCompartment* comp,
char* buf, size_t bufsize)
GetRealmNameCallback(JSContext* cx, Handle<Realm*> realm,
char* buf, size_t bufsize)
{
nsCString name;
// This is called via the JSAPI and isn't involved in memory reporting, so
// we don't need to anonymize compartment names.
// we don't need to anonymize realm names.
int anonymizeID = 0;
GetCompartmentName(comp, name, &anonymizeID, /* replaceSlashes = */ false);
GetRealmName(realm, name, &anonymizeID, /* replaceSlashes = */ false);
if (name.Length() >= bufsize)
name.Truncate(bufsize - 1);
memcpy(buf, name.get(), name.Length() + 1);
@ -2665,13 +2666,6 @@ DestroyRealm(JSFreeOp* fop, JS::Realm* realm)
JS::SetRealmPrivate(realm, nullptr);
}
static void
GetRealmName(JSContext* cx, Handle<Realm*> realm, char* buf, size_t bufsize)
{
JSCompartment* comp = GetCompartmentForRealm(realm);
CompartmentNameCallback(cx, comp, buf, bufsize);
}
static bool
PreserveWrapper(JSContext* cx, JSObject* obj)
{
@ -2841,9 +2835,8 @@ XPCJSRuntime::Initialize(JSContext* cx)
JS_SetDestroyCompartmentCallback(cx, CompartmentDestroyedCallback);
JS_SetSizeOfIncludingThisCompartmentCallback(cx, CompartmentSizeOfIncludingThisCallback);
JS_SetCompartmentNameCallback(cx, CompartmentNameCallback);
JS::SetDestroyRealmCallback(cx, DestroyRealm);
JS::SetRealmNameCallback(cx, GetRealmName);
JS::SetRealmNameCallback(cx, GetRealmNameCallback);
mPrevGCSliceCallback = JS::SetGCSliceCallback(cx, GCSliceCallback);
mPrevDoCycleCollectionCallback = JS::SetDoCycleCollectionCallback(cx,
DoCycleCollectionCallback);

View File

@ -615,11 +615,11 @@ JSObject*
FindExceptionStackForConsoleReport(nsPIDOMWindowInner* win,
JS::HandleValue exceptionValue);
// Return a name for the compartment.
// Return a name for the realm.
// This function makes reasonable efforts to make this name both mostly human-readable
// and unique. However, there are no guarantees of either property.
extern void
GetCurrentCompartmentName(JSContext*, nsCString& name);
GetCurrentRealmName(JSContext*, nsCString& name);
void AddGCCallback(xpcGCCallback cb);
void RemoveGCCallback(xpcGCCallback cb);

View File

@ -110,7 +110,7 @@ URLForGlobal(JSContext* cx, JS::Handle<JSObject*> global, nsAString& url) {
* Extract a somewhat human-readable name from the current context.
*/
void
CompartmentName(JSContext* cx, JS::Handle<JSObject*> global, nsAString& name) {
RealmName(JSContext* cx, JS::Handle<JSObject*> global, nsAString& name) {
// Attempt to use the URL as name.
if (URLForGlobal(cx, global, name)) {
return;
@ -119,7 +119,7 @@ CompartmentName(JSContext* cx, JS::Handle<JSObject*> global, nsAString& name) {
// Otherwise, fallback to XPConnect's less readable but more
// complete naming scheme.
nsAutoCString cname;
xpc::GetCurrentCompartmentName(cx, cname);
xpc::GetCurrentRealmName(cx, cname);
name.Assign(NS_ConvertUTF8toUTF16(cname));
}
@ -1035,7 +1035,7 @@ nsPerformanceStatsService::GetPerformanceGroups(JSContext* cx,
}
nsAutoString name;
CompartmentName(cx, global, name);
RealmName(cx, global, name);
bool isSystem = nsContentUtils::IsSystemPrincipal(nsContentUtils::ObjectPrincipal(global));
// Find out if the compartment is executed by a window. If so, its