1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
/* Class used to manage the wrapped native objects within a JS scope. */
#include "AccessCheck.h"
#include "xpcprivate.h"
#include "XPCWrapper.h"
#include "nsContentUtils.h"
#include "nsCycleCollectionNoteRootCallback.h"
#include "ExpandedPrincipal.h"
#include "mozilla/BasePrincipal.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/Preferences.h"
#include "mozilla/Unused.h"
#include "mozJSComponentLoader.h"
#include "js/Object.h" // JS::GetCompartment
#include "mozilla/dom/BindingUtils.h"
using namespace mozilla;
using namespace xpc;
using namespace JS;
/***************************************************************************/
static XPCWrappedNativeScopeList& AllScopes() {
return XPCJSRuntime::Get()->GetWrappedNativeScopes();
}
static bool RemoteXULForbidsXBLScopeForPrincipal(nsIPrincipal* aPrincipal) {
// AllowXULXBLForPrincipal will return true for system principal, but we
// don't want that here.
MOZ_ASSERT(nsContentUtils::IsInitialized());
if (aPrincipal->IsSystemPrincipal()) {
return false;
}
// If this domain isn't whitelisted, we're done.
if (!nsContentUtils::AllowXULXBLForPrincipal(aPrincipal)) {
return false;
}
// Check the pref to determine how we should behave.
return !Preferences::GetBool("dom.use_xbl_scopes_for_remote_xul", false);
}
static bool RemoteXULForbidsXBLScope(HandleObject aFirstGlobal) {
MOZ_ASSERT(aFirstGlobal);
// Certain singleton sandoxes are created very early in startup - too early
// to call into AllowXULXBLForPrincipal. We never create XBL scopes for
// sandboxes anway, and certainly not for these singleton scopes. So we just
// short-circuit here.
if (IsSandbox(aFirstGlobal)) {
return false;
}
nsIPrincipal* principal = xpc::GetObjectPrincipal(aFirstGlobal);
return RemoteXULForbidsXBLScopeForPrincipal(principal);
}
XPCWrappedNativeScope::XPCWrappedNativeScope(JS::Compartment* aCompartment,
JS::HandleObject aFirstGlobal)
: mWrappedNativeMap(mozilla::MakeUnique<Native2WrappedNativeMap>()),
mWrappedNativeProtoMap(
mozilla::MakeUnique<ClassInfo2WrappedNativeProtoMap>()),
mComponents(nullptr),
mCompartment(aCompartment) {
#ifdef DEBUG
for (XPCWrappedNativeScope* cur : AllScopes()) {
MOZ_ASSERT(aCompartment != cur->Compartment(), "dup object");
}
#endif
AllScopes().insertBack(this);
MOZ_COUNT_CTOR(XPCWrappedNativeScope);
// Determine whether we would allow an XBL scope in this situation.
// In addition to being pref-controlled, we also disable XBL scopes for
// remote XUL domains, _except_ if we have an additional pref override set.
//
// Note that we can't quite remove this yet, even though we never actually
// use XBL scopes, because the security manager uses this boolean to make
// decisions that we rely on in our test infrastructure.
//
// FIXME(emilio): Now that the security manager is the only caller probably
// should be renamed, but what's a good name for this?
mAllowContentXBLScope = !RemoteXULForbidsXBLScope(aFirstGlobal);
}
bool XPCWrappedNativeScope::GetComponentsJSObject(JSContext* cx,
JS::MutableHandleObject obj) {
if (!mComponents) {
bool system = AccessCheck::isChrome(mCompartment);
MOZ_RELEASE_ASSERT(system, "How did we get a non-system Components?");
mComponents = new nsXPCComponents(this);
}
RootedValue val(cx);
xpcObjectHelper helper(mComponents);
bool ok = XPCConvert::NativeInterface2JSObject(cx, &val, helper, nullptr,
false, nullptr);
if (NS_WARN_IF(!ok)) {
return false;
}
if (NS_WARN_IF(!val.isObject())) {
return false;
}
obj.set(&val.toObject());
return true;
}
static bool DefineSubcomponentProperty(JSContext* aCx, HandleObject aGlobal,
nsISupports* aSubcomponent,
const nsID* aIID,
unsigned int aStringIndex) {
RootedValue subcompVal(aCx);
xpcObjectHelper helper(aSubcomponent);
if (!XPCConvert::NativeInterface2JSObject(aCx, &subcompVal, helper, aIID,
false, nullptr))
return false;
if (NS_WARN_IF(!subcompVal.isObject())) {
return false;
}
RootedId id(aCx, XPCJSContext::Get()->GetStringID(aStringIndex));
return JS_DefinePropertyById(aCx, aGlobal, id, subcompVal, 0);
}
bool XPCWrappedNativeScope::AttachComponentsObject(JSContext* aCx) {
RootedObject components(aCx);
if (!GetComponentsJSObject(aCx, &components)) {
return false;
}
RootedObject global(aCx, CurrentGlobalOrNull(aCx));
const unsigned attrs = JSPROP_READONLY | JSPROP_RESOLVING | JSPROP_PERMANENT;
RootedId id(aCx,
XPCJSContext::Get()->GetStringID(XPCJSContext::IDX_COMPONENTS));
if (!JS_DefinePropertyById(aCx, global, id, components, attrs)) {
return false;
}
// _iid can be nullptr if the object implements classinfo.
#define DEFINE_SUBCOMPONENT_PROPERTY(_comp, _type, _iid, _id) \
nsCOMPtr<nsIXPCComponents_##_type> obj##_type; \
if (NS_FAILED(_comp->Get##_type(getter_AddRefs(obj##_type)))) return false; \
if (!DefineSubcomponentProperty(aCx, global, obj##_type, _iid, \
XPCJSContext::IDX_##_id)) \
return false;
DEFINE_SUBCOMPONENT_PROPERTY(mComponents, Interfaces, nullptr, CI)
DEFINE_SUBCOMPONENT_PROPERTY(mComponents, Results, nullptr, CR)
DEFINE_SUBCOMPONENT_PROPERTY(mComponents, Classes, nullptr, CC)
DEFINE_SUBCOMPONENT_PROPERTY(mComponents, Utils,
&NS_GET_IID(nsIXPCComponents_Utils), CU)
#undef DEFINE_SUBCOMPONENT_PROPERTY
return true;
}
bool XPCWrappedNativeScope::XBLScopeStateMatches(nsIPrincipal* aPrincipal) {
return mAllowContentXBLScope ==
!RemoteXULForbidsXBLScopeForPrincipal(aPrincipal);
}
bool XPCWrappedNativeScope::AllowContentXBLScope(Realm* aRealm) {
// We only disallow XBL scopes in remote XUL situations.
MOZ_ASSERT_IF(!mAllowContentXBLScope, nsContentUtils::AllowXULXBLForPrincipal(
xpc::GetRealmPrincipal(aRealm)));
return mAllowContentXBLScope;
}
namespace xpc {
JSObject* GetUAWidgetScope(JSContext* cx, JSObject* contentScopeArg) {
JS::RootedObject contentScope(cx, contentScopeArg);
JSAutoRealm ar(cx, contentScope);
nsIPrincipal* principal = GetObjectPrincipal(contentScope);
if (principal->IsSystemPrincipal()) {
return JS::GetNonCCWObjectGlobal(contentScope);
}
return GetUAWidgetScope(cx, principal);
}
JSObject* GetUAWidgetScope(JSContext* cx, nsIPrincipal* principal) {
RootedObject scope(cx, XPCJSRuntime::Get()->GetUAWidgetScope(cx, principal));
NS_ENSURE_TRUE(scope, nullptr); // See bug 858642.
scope = js::UncheckedUnwrap(scope);
JS::ExposeObjectToActiveJS(scope);
return scope;
}
bool AllowContentXBLScope(JS::Realm* realm) {
JS::Compartment* comp = GetCompartmentForRealm(realm);
XPCWrappedNativeScope* scope = CompartmentPrivate::Get(comp)->GetScope();
MOZ_ASSERT(scope);
return scope->AllowContentXBLScope(realm);
}
} /* namespace xpc */
XPCWrappedNativeScope::~XPCWrappedNativeScope() {
MOZ_COUNT_DTOR(XPCWrappedNativeScope);
// We can do additional cleanup assertions here...
MOZ_ASSERT(0 == mWrappedNativeMap->Count(), "scope has non-empty map");
MOZ_ASSERT(0 == mWrappedNativeProtoMap->Count(), "scope has non-empty map");
// This should not be necessary, since the Components object should die
// with the scope but just in case.
if (mComponents) {
mComponents->mScope = nullptr;
}
// XXX we should assert that we are dead or that xpconnect has shutdown
// XXX might not want to do this at xpconnect shutdown time???
mComponents = nullptr;
MOZ_RELEASE_ASSERT(!mXrayExpandos.initialized());
mCompartment = nullptr;
}
// static
void XPCWrappedNativeScope::TraceWrappedNativesInAllScopes(XPCJSRuntime* xpcrt,
JSTracer* trc) {
// Do JS::TraceEdge for all wrapped natives with external references, as
// well as any DOM expando objects.
//
// Note: the GC can call this from a JS helper thread. We don't use
// AllScopes() because that asserts we're on the main thread.
for (XPCWrappedNativeScope* cur : xpcrt->GetWrappedNativeScopes()) {
for (auto i = cur->mWrappedNativeMap->Iter(); !i.Done(); i.Next()) {
auto entry = static_cast<Native2WrappedNativeMap::Entry*>(i.Get());
XPCWrappedNative* wrapper = entry->value;
if (wrapper->HasExternalReference() && !wrapper->IsWrapperExpired()) {
wrapper->TraceSelf(trc);
}
}
}
}
// static
void XPCWrappedNativeScope::SuspectAllWrappers(
nsCycleCollectionNoteRootCallback& cb) {
for (XPCWrappedNativeScope* cur : AllScopes()) {
for (auto i = cur->mWrappedNativeMap->Iter(); !i.Done(); i.Next()) {
static_cast<Native2WrappedNativeMap::Entry*>(i.Get())->value->Suspect(cb);
}
}
}
void XPCWrappedNativeScope::UpdateWeakPointersAfterGC() {
// Sweep waivers.
if (mWaiverWrapperMap) {
mWaiverWrapperMap->Sweep();
}
if (!js::IsCompartmentZoneSweepingOrCompacting(mCompartment)) {
return;
}
if (!js::CompartmentHasLiveGlobal(mCompartment)) {
GetWrappedNativeMap()->Clear();
mWrappedNativeProtoMap->Clear();
// The fields below are traced only if there's a live global in the
// compartment, see TraceXPCGlobal. The compartment has no live globals so
// clear these pointers here.
if (mXrayExpandos.initialized()) {
mXrayExpandos.destroy();
}
mIDProto = nullptr;
mIIDProto = nullptr;
mCIDProto = nullptr;
return;
}
// Sweep mWrappedNativeMap for dying flat JS objects. Moving has already
// been handled by XPCWrappedNative::FlatJSObjectMoved.
for (auto iter = GetWrappedNativeMap()->Iter(); !iter.Done(); iter.Next()) {
auto entry = static_cast<Native2WrappedNativeMap::Entry*>(iter.Get());
XPCWrappedNative* wrapper = entry->value;
JSObject* obj = wrapper->GetFlatJSObjectPreserveColor();
JS_UpdateWeakPointerAfterGCUnbarriered(&obj);
MOZ_ASSERT(!obj || obj == wrapper->GetFlatJSObjectPreserveColor());
MOZ_ASSERT_IF(obj, JS::GetCompartment(obj) == mCompartment);
if (!obj) {
iter.Remove();
}
}
// Sweep mWrappedNativeProtoMap for dying prototype JSObjects. Moving has
// already been handled by XPCWrappedNativeProto::JSProtoObjectMoved.
for (auto i = mWrappedNativeProtoMap->Iter(); !i.Done(); i.Next()) {
auto entry = static_cast<ClassInfo2WrappedNativeProtoMap::Entry*>(i.Get());
JSObject* obj = entry->value->GetJSProtoObjectPreserveColor();
JS_UpdateWeakPointerAfterGCUnbarriered(&obj);
MOZ_ASSERT_IF(obj, JS::GetCompartment(obj) == mCompartment);
MOZ_ASSERT(!obj || obj == entry->value->GetJSProtoObjectPreserveColor());
if (!obj) {
i.Remove();
}
}
}
// static
void XPCWrappedNativeScope::SweepAllWrappedNativeTearOffs() {
for (XPCWrappedNativeScope* cur : AllScopes()) {
for (auto i = cur->mWrappedNativeMap->Iter(); !i.Done(); i.Next()) {
auto entry = static_cast<Native2WrappedNativeMap::Entry*>(i.Get());
entry->value->SweepTearOffs();
}
}
}
// static
void XPCWrappedNativeScope::SystemIsBeingShutDown() {
// We're forcibly killing scopes, rather than allowing them to go away
// when they're ready. As such, we need to do some cleanup before they
// can safely be destroyed.
for (XPCWrappedNativeScope* cur : AllScopes()) {
// Give the Components object a chance to try to clean up.
if (cur->mComponents) {
cur->mComponents->SystemIsBeingShutDown();
}
// Null out these pointers to prevent ~ObjectPtr assertion failures if we
// leaked things at shutdown.
cur->mIDProto = nullptr;
cur->mIIDProto = nullptr;
cur->mCIDProto = nullptr;
// Similarly, destroy mXrayExpandos to prevent assertion failures.
if (cur->mXrayExpandos.initialized()) {
cur->mXrayExpandos.destroy();
}
// Walk the protos first. Wrapper shutdown can leave dangling
// proto pointers in the proto map.
for (auto i = cur->mWrappedNativeProtoMap->Iter(); !i.Done(); i.Next()) {
auto entry =
static_cast<ClassInfo2WrappedNativeProtoMap::Entry*>(i.Get());
entry->value->SystemIsBeingShutDown();
i.Remove();
}
for (auto i = cur->mWrappedNativeMap->Iter(); !i.Done(); i.Next()) {
auto entry = static_cast<Native2WrappedNativeMap::Entry*>(i.Get());
entry->value->SystemIsBeingShutDown();
i.Remove();
}
CompartmentPrivate* priv = CompartmentPrivate::Get(cur->Compartment());
priv->SystemIsBeingShutDown();
}
}
/***************************************************************************/
JSObject* XPCWrappedNativeScope::GetExpandoChain(HandleObject target) {
MOZ_ASSERT(ObjectScope(target) == this);
if (!mXrayExpandos.initialized()) {
return nullptr;
}
return mXrayExpandos.lookup(target);
}
JSObject* XPCWrappedNativeScope::DetachExpandoChain(HandleObject target) {
MOZ_ASSERT(ObjectScope(target) == this);
if (!mXrayExpandos.initialized()) {
return nullptr;
}
return mXrayExpandos.removeValue(target);
}
bool XPCWrappedNativeScope::SetExpandoChain(JSContext* cx, HandleObject target,
HandleObject chain) {
MOZ_ASSERT(ObjectScope(target) == this);
MOZ_ASSERT(js::IsObjectInContextCompartment(target, cx));
MOZ_ASSERT_IF(chain, ObjectScope(chain) == this);
if (!mXrayExpandos.initialized() && !mXrayExpandos.init(cx)) {
return false;
}
return mXrayExpandos.put(cx, target, chain);
}
/***************************************************************************/
// static
void XPCWrappedNativeScope::DebugDumpAllScopes(int16_t depth) {
#ifdef DEBUG
depth--;
// get scope count.
int count = 0;
for (XPCWrappedNativeScope* cur : AllScopes()) {
mozilla::Unused << cur;
count++;
}
XPC_LOG_ALWAYS(("chain of %d XPCWrappedNativeScope(s)", count));
XPC_LOG_INDENT();
if (depth) {
for (XPCWrappedNativeScope* cur : AllScopes()) {
cur->DebugDump(depth);
}
}
XPC_LOG_OUTDENT();
#endif
}
void XPCWrappedNativeScope::DebugDump(int16_t depth) {
#ifdef DEBUG
depth--;
XPC_LOG_ALWAYS(("XPCWrappedNativeScope @ %p", this));
XPC_LOG_INDENT();
XPC_LOG_ALWAYS(("next @ %p", getNext()));
XPC_LOG_ALWAYS(("mComponents @ %p", mComponents.get()));
XPC_LOG_ALWAYS(("mCompartment @ %p", mCompartment));
XPC_LOG_ALWAYS(("mWrappedNativeMap @ %p with %d wrappers(s)",
mWrappedNativeMap.get(), mWrappedNativeMap->Count()));
// iterate contexts...
if (depth && mWrappedNativeMap->Count()) {
XPC_LOG_INDENT();
for (auto i = mWrappedNativeMap->Iter(); !i.Done(); i.Next()) {
auto entry = static_cast<Native2WrappedNativeMap::Entry*>(i.Get());
entry->value->DebugDump(depth);
}
XPC_LOG_OUTDENT();
}
XPC_LOG_ALWAYS(("mWrappedNativeProtoMap @ %p with %d protos(s)",
mWrappedNativeProtoMap.get(),
mWrappedNativeProtoMap->Count()));
// iterate contexts...
if (depth && mWrappedNativeProtoMap->Count()) {
XPC_LOG_INDENT();
for (auto i = mWrappedNativeProtoMap->Iter(); !i.Done(); i.Next()) {
auto entry =
static_cast<ClassInfo2WrappedNativeProtoMap::Entry*>(i.Get());
entry->value->DebugDump(depth);
}
XPC_LOG_OUTDENT();
}
XPC_LOG_OUTDENT();
#endif
}
void XPCWrappedNativeScope::AddSizeOfAllScopesIncludingThis(
JSContext* cx, ScopeSizeInfo* scopeSizeInfo) {
for (XPCWrappedNativeScope* cur : AllScopes()) {
cur->AddSizeOfIncludingThis(cx, scopeSizeInfo);
}
}
void XPCWrappedNativeScope::AddSizeOfIncludingThis(
JSContext* cx, ScopeSizeInfo* scopeSizeInfo) {
scopeSizeInfo->mScopeAndMapSize += scopeSizeInfo->mMallocSizeOf(this);
scopeSizeInfo->mScopeAndMapSize +=
mWrappedNativeMap->SizeOfIncludingThis(scopeSizeInfo->mMallocSizeOf);
scopeSizeInfo->mScopeAndMapSize +=
mWrappedNativeProtoMap->SizeOfIncludingThis(scopeSizeInfo->mMallocSizeOf);
auto realmCb = [](JSContext*, void* aData, JS::Realm* aRealm,
const JS::AutoRequireNoGC& nogc) {
auto* scopeSizeInfo = static_cast<ScopeSizeInfo*>(aData);
JSObject* global = GetRealmGlobalOrNull(aRealm);
if (global && dom::HasProtoAndIfaceCache(global)) {
dom::ProtoAndIfaceCache* cache = dom::GetProtoAndIfaceCache(global);
scopeSizeInfo->mProtoAndIfaceCacheSize +=
cache->SizeOfIncludingThis(scopeSizeInfo->mMallocSizeOf);
}
};
IterateRealmsInCompartment(cx, Compartment(), scopeSizeInfo, realmCb);
// There are other XPCWrappedNativeScope members that could be measured;
// the above ones have been seen by DMD to be worth measuring. More stuff
// may be added later.
}
|