tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

XPCWrappedNative.cpp (62254B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 /* Wrapper object for reflecting native xpcom objects into JavaScript. */
      8 
      9 #include "xpcprivate.h"
     10 #include "XPCMaps.h"
     11 #include "nsWrapperCacheInlines.h"
     12 #include "XPCLog.h"
     13 #include "js/Array.h"                   // JS::GetArrayLength, JS::IsArrayObject
     14 #include "js/experimental/TypedData.h"  // JS_GetTypedArrayLength, JS_IsTypedArrayObject
     15 #include "js/MemoryFunctions.h"
     16 #include "js/Object.h"  // JS::GetPrivate, JS::SetPrivate, JS::SetReservedSlot
     17 #include "js/Printf.h"
     18 #include "js/PropertyAndElement.h"  // JS_GetProperty, JS_GetPropertyById, JS_SetProperty, JS_SetPropertyById
     19 #include "jsfriendapi.h"
     20 #include "AccessCheck.h"
     21 #include "WrapperFactory.h"
     22 #include "XrayWrapper.h"
     23 
     24 #include "nsContentUtils.h"
     25 #include "nsCycleCollectionNoteRootCallback.h"
     26 
     27 #include <new>
     28 #include <stdint.h>
     29 #include "mozilla/DeferredFinalize.h"
     30 #include "mozilla/Likely.h"
     31 #include "mozilla/Sprintf.h"
     32 #include "mozilla/dom/BindingUtils.h"
     33 #include "mozilla/ProfilerLabels.h"
     34 #include <algorithm>
     35 
     36 using namespace xpc;
     37 using namespace mozilla;
     38 using namespace mozilla::dom;
     39 using namespace JS;
     40 
     41 /***************************************************************************/
     42 
     43 NS_IMPL_CYCLE_COLLECTION_CLASS(XPCWrappedNative)
     44 
     45 // No need to unlink the JS objects: if the XPCWrappedNative is cycle
     46 // collected then its mFlatJSObject will be cycle collected too and
     47 // finalization of the mFlatJSObject will unlink the JS objects (see
     48 // XPC_WN_NoHelper_Finalize and FlatJSObjectFinalized).
     49 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(XPCWrappedNative)
     50  tmp->ExpireWrapper();
     51 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
     52 
     53 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INTERNAL(XPCWrappedNative)
     54  if (!tmp->IsValid()) {
     55    return NS_OK;
     56  }
     57 
     58  if (MOZ_UNLIKELY(cb.WantDebugInfo())) {
     59    char name[72];
     60    nsCOMPtr<nsIXPCScriptable> scr = tmp->GetScriptable();
     61    if (scr) {
     62      SprintfLiteral(name, "XPCWrappedNative (%s)", scr->GetJSClass()->name);
     63    } else {
     64      SprintfLiteral(name, "XPCWrappedNative");
     65    }
     66 
     67    cb.DescribeRefCountedNode(tmp->mRefCnt.get(), name);
     68  } else {
     69    NS_IMPL_CYCLE_COLLECTION_DESCRIBE(XPCWrappedNative, tmp->mRefCnt.get())
     70  }
     71 
     72  if (tmp->HasExternalReference()) {
     73    // If our refcount is > 1, our reference to the flat JS object is
     74    // considered "strong", and we're going to traverse it.
     75    //
     76    // If our refcount is <= 1, our reference to the flat JS object is
     77    // considered "weak", and we're *not* going to traverse it.
     78    //
     79    // This reasoning is in line with the slightly confusing lifecycle rules
     80    // for XPCWrappedNatives, described in a larger comment below and also
     81    // on our wiki at http://wiki.mozilla.org/XPConnect_object_wrapping
     82 
     83    JSObject* obj = tmp->GetFlatJSObjectPreserveColor();
     84    NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mFlatJSObject");
     85    cb.NoteJSChild(JS::GCCellPtr(obj));
     86  }
     87 
     88  // XPCWrappedNative keeps its native object alive.
     89  NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mIdentity");
     90  cb.NoteXPCOMChild(tmp->GetIdentityObject());
     91 
     92  tmp->NoteTearoffs(cb);
     93 
     94 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
     95 
     96 void XPCWrappedNative::Suspect(nsCycleCollectionNoteRootCallback& cb) {
     97  if (!IsValid() || IsWrapperExpired()) {
     98    return;
     99  }
    100 
    101  MOZ_ASSERT(NS_IsMainThread(),
    102             "Suspecting wrapped natives from non-main thread");
    103 
    104  // Only record objects that might be part of a cycle as roots, unless
    105  // the callback wants all traces (a debug feature). Do this even if
    106  // the XPCWN doesn't own the JS reflector object in case the reflector
    107  // keeps alive other C++ things. This is safe because if the reflector
    108  // had died the reference from the XPCWN to it would have been cleared.
    109  JSObject* obj = GetFlatJSObjectPreserveColor();
    110  if (JS::ObjectIsMarkedGray(obj) || cb.WantAllTraces()) {
    111    cb.NoteJSRoot(obj);
    112  }
    113 }
    114 
    115 void XPCWrappedNative::NoteTearoffs(nsCycleCollectionTraversalCallback& cb) {
    116  // Tearoffs hold their native object alive. If their JS object hasn't been
    117  // finalized yet we'll note the edge between the JS object and the native
    118  // (see nsXPConnect::Traverse), but if their JS object has been finalized
    119  // then the tearoff is only reachable through the XPCWrappedNative, so we
    120  // record an edge here.
    121  for (XPCWrappedNativeTearOff* to = &mFirstTearOff; to;
    122       to = to->GetNextTearOff()) {
    123    JSObject* jso = to->GetJSObjectPreserveColor();
    124    if (!jso) {
    125      NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "tearoff's mNative");
    126      cb.NoteXPCOMChild(to->GetNative());
    127    }
    128  }
    129 }
    130 
    131 #ifdef XPC_CHECK_CLASSINFO_CLAIMS
    132 static void DEBUG_CheckClassInfoClaims(XPCWrappedNative* wrapper);
    133 #else
    134 #  define DEBUG_CheckClassInfoClaims(wrapper) ((void)0)
    135 #endif
    136 
    137 /***************************************************************************/
    138 static nsresult FinishCreate(JSContext* cx, XPCWrappedNativeScope* Scope,
    139                             XPCNativeInterface* Interface,
    140                             nsWrapperCache* cache, XPCWrappedNative* inWrapper,
    141                             XPCWrappedNative** resultWrapper);
    142 
    143 // static
    144 //
    145 // This method handles the special case of wrapping a new global object.
    146 //
    147 // The normal code path for wrapping natives goes through
    148 // XPCConvert::NativeInterface2JSObject, XPCWrappedNative::GetNewOrUsed,
    149 // and finally into XPCWrappedNative::Init. Unfortunately, this path assumes
    150 // very early on that we have an XPCWrappedNativeScope and corresponding global
    151 // JS object, which are the very things we need to create here. So we special-
    152 // case the logic and do some things in a different order.
    153 nsresult XPCWrappedNative::WrapNewGlobal(JSContext* cx,
    154                                         xpcObjectHelper& nativeHelper,
    155                                         nsIPrincipal* principal,
    156                                         JS::RealmOptions& aOptions,
    157                                         XPCWrappedNative** wrappedGlobal) {
    158  nsCOMPtr<nsISupports> identity = do_QueryInterface(nativeHelper.Object());
    159 
    160  // The object should specify that it's meant to be global.
    161  MOZ_ASSERT(nativeHelper.GetScriptableFlags() &
    162             XPC_SCRIPTABLE_IS_GLOBAL_OBJECT);
    163 
    164  // We shouldn't be reusing globals.
    165  MOZ_ASSERT(!nativeHelper.GetWrapperCache() ||
    166             !nativeHelper.GetWrapperCache()->GetWrapperPreserveColor());
    167 
    168  // Get the nsIXPCScriptable. This will tell us the JSClass of the object
    169  // we're going to create.
    170  nsCOMPtr<nsIXPCScriptable> scrProto;
    171  nsCOMPtr<nsIXPCScriptable> scrWrapper;
    172  GatherScriptable(identity, nativeHelper.GetClassInfo(),
    173                   getter_AddRefs(scrProto), getter_AddRefs(scrWrapper));
    174  MOZ_ASSERT(scrWrapper);
    175 
    176  // Finally, we get to the JSClass.
    177  const JSClass* clasp = scrWrapper->GetJSClass();
    178  MOZ_ASSERT(clasp->flags & JSCLASS_IS_GLOBAL);
    179 
    180  // Create the global.
    181  aOptions.creationOptions().setTrace(XPCWrappedNative::Trace);
    182  xpc::SetPrefableRealmOptions(aOptions);
    183 
    184  RootedObject global(cx,
    185                      xpc::CreateGlobalObject(cx, clasp, principal, aOptions));
    186  if (!global) {
    187    return NS_ERROR_FAILURE;
    188  }
    189  XPCWrappedNativeScope* scope = ObjectScope(global);
    190 
    191  // Immediately enter the global's realm, so that everything else we
    192  // create ends up there.
    193  JSAutoRealm ar(cx, global);
    194 
    195  // Make a proto.
    196  XPCWrappedNativeProto* proto = XPCWrappedNativeProto::GetNewOrUsed(
    197      cx, scope, nativeHelper.GetClassInfo(), scrProto);
    198  if (!proto) {
    199    return NS_ERROR_FAILURE;
    200  }
    201 
    202  // Set up the prototype on the global.
    203  MOZ_ASSERT(proto->GetJSProtoObject());
    204  RootedObject protoObj(cx, proto->GetJSProtoObject());
    205  bool success = JS_SetPrototype(cx, global, protoObj);
    206  if (!success) {
    207    return NS_ERROR_FAILURE;
    208  }
    209 
    210  // Construct the wrapper, which takes over the strong reference to the
    211  // native object.
    212  RefPtr<XPCWrappedNative> wrapper =
    213      new XPCWrappedNative(std::move(identity), proto);
    214 
    215  //
    216  // We don't call ::Init() on this wrapper, because our setup requirements
    217  // are different for globals. We do our setup inline here, instead.
    218  //
    219 
    220  wrapper->mScriptable = scrWrapper;
    221 
    222  // Set the JS object to the global we already created.
    223  wrapper->SetFlatJSObject(global);
    224 
    225  // Set the reserved slot to the XPCWrappedNative.
    226  static_assert(JSCLASS_GLOBAL_APPLICATION_SLOTS > 0,
    227                "Need at least one slot for JSCLASS_SLOT0_IS_NSISUPPORTS");
    228  JS::SetObjectISupports(global, wrapper);
    229 
    230  // There are dire comments elsewhere in the code about how a GC can
    231  // happen somewhere after wrapper initialization but before the wrapper is
    232  // added to the hashtable in FinishCreate(). It's not clear if that can
    233  // happen here, but let's just be safe for now.
    234  AutoMarkingWrappedNativePtr wrapperMarker(cx, wrapper);
    235 
    236  // Call the common Init finish routine. This mainly just does an AddRef
    237  // on behalf of XPConnect (the corresponding Release is in the finalizer
    238  // hook), but it does some other miscellaneous things too, so we don't
    239  // inline it.
    240  success = wrapper->FinishInit(cx);
    241  MOZ_ASSERT(success);
    242 
    243  // Go through some extra work to find the tearoff. This is kind of silly
    244  // on a conceptual level: the point of tearoffs is to cache the results
    245  // of QI-ing mIdentity to different interfaces, and we don't need that
    246  // since we're dealing with nsISupports. But lots of code expects tearoffs
    247  // to exist for everything, so we just follow along.
    248  RefPtr<XPCNativeInterface> iface =
    249      XPCNativeInterface::GetNewOrUsed(cx, &NS_GET_IID(nsISupports));
    250  MOZ_ASSERT(iface);
    251  nsresult status;
    252  success = wrapper->FindTearOff(cx, iface, false, &status);
    253  if (!success) {
    254    return status;
    255  }
    256 
    257  // Call the common creation finish routine. This does all of the bookkeeping
    258  // like inserting the wrapper into the wrapper map and setting up the wrapper
    259  // cache.
    260  nsresult rv = FinishCreate(cx, scope, iface, nativeHelper.GetWrapperCache(),
    261                             wrapper, wrappedGlobal);
    262  NS_ENSURE_SUCCESS(rv, rv);
    263 
    264  return NS_OK;
    265 }
    266 
    267 // static
    268 nsresult XPCWrappedNative::GetNewOrUsed(JSContext* cx, xpcObjectHelper& helper,
    269                                        XPCWrappedNativeScope* Scope,
    270                                        XPCNativeInterface* Interface,
    271                                        XPCWrappedNative** resultWrapper) {
    272  MOZ_ASSERT(Interface);
    273  nsWrapperCache* cache = helper.GetWrapperCache();
    274 
    275  MOZ_ASSERT(!cache || !cache->GetWrapperPreserveColor(),
    276             "We assume the caller already checked if it could get the "
    277             "wrapper from the cache.");
    278 
    279  nsresult rv;
    280 
    281  MOZ_ASSERT(!Scope->GetRuntime()->GCIsRunning(),
    282             "XPCWrappedNative::GetNewOrUsed called during GC");
    283 
    284  nsCOMPtr<nsISupports> identity = do_QueryInterface(helper.Object());
    285 
    286  if (!identity) {
    287    NS_ERROR("This XPCOM object fails in QueryInterface to nsISupports!");
    288    return NS_ERROR_FAILURE;
    289  }
    290 
    291  RefPtr<XPCWrappedNative> wrapper;
    292 
    293  Native2WrappedNativeMap* map = Scope->GetWrappedNativeMap();
    294  // Some things are nsWrapperCache subclasses but never use the cache, so go
    295  // ahead and check our map even if we have a cache and it has no existing
    296  // wrapper: we might have an XPCWrappedNative anyway.
    297  wrapper = map->Find(identity);
    298 
    299  if (wrapper) {
    300    if (!wrapper->FindTearOff(cx, Interface, false, &rv)) {
    301      MOZ_ASSERT(NS_FAILED(rv), "returning NS_OK on failure");
    302      return rv;
    303    }
    304    wrapper.forget(resultWrapper);
    305    return NS_OK;
    306  }
    307 
    308  // There is a chance that the object wants to have the self-same JSObject
    309  // reflection regardless of the scope into which we are reflecting it.
    310  // Many DOM objects require this. The scriptable helper specifies this
    311  // in preCreate by indicating a 'parent' of a particular scope.
    312  //
    313  // To handle this we need to get the scriptable helper early and ask it.
    314  // It is possible that we will then end up forwarding this entire call
    315  // to this same function but with a different scope.
    316 
    317  // If we are making a wrapper for an nsIClassInfo singleton then
    318  // We *don't* want to have it use the prototype meant for instances
    319  // of that class.
    320  uint32_t classInfoFlags;
    321  bool isClassInfoSingleton =
    322      helper.GetClassInfo() == helper.Object() &&
    323      NS_SUCCEEDED(helper.GetClassInfo()->GetFlags(&classInfoFlags)) &&
    324      (classInfoFlags & nsIClassInfo::SINGLETON_CLASSINFO);
    325 
    326  nsIClassInfo* info = helper.GetClassInfo();
    327 
    328  nsCOMPtr<nsIXPCScriptable> scrProto;
    329  nsCOMPtr<nsIXPCScriptable> scrWrapper;
    330 
    331  // Gather scriptable create info if we are wrapping something
    332  // other than an nsIClassInfo object. We need to not do this for
    333  // nsIClassInfo objects because often nsIClassInfo implementations
    334  // are also nsIXPCScriptable helper implementations, but the helper
    335  // code is obviously intended for the implementation of the class
    336  // described by the nsIClassInfo, not for the class info object
    337  // itself.
    338  if (!isClassInfoSingleton) {
    339    GatherScriptable(identity, info, getter_AddRefs(scrProto),
    340                     getter_AddRefs(scrWrapper));
    341  }
    342 
    343  RootedObject parent(cx, Scope->GetGlobalForWrappedNatives());
    344 
    345  mozilla::Maybe<JSAutoRealm> ar;
    346 
    347  if (scrWrapper && scrWrapper->WantPreCreate()) {
    348    RootedObject plannedParent(cx, parent);
    349    nsresult rv = scrWrapper->PreCreate(identity, cx, parent, parent.address());
    350    if (NS_FAILED(rv)) {
    351      return rv;
    352    }
    353    rv = NS_OK;
    354 
    355    MOZ_ASSERT(!xpc::WrapperFactory::IsXrayWrapper(parent),
    356               "Xray wrapper being used to parent XPCWrappedNative?");
    357 
    358    MOZ_ASSERT(JS_IsGlobalObject(parent),
    359               "Non-global being used to parent XPCWrappedNative?");
    360 
    361    ar.emplace(static_cast<JSContext*>(cx), parent);
    362 
    363    if (parent != plannedParent) {
    364      XPCWrappedNativeScope* betterScope = ObjectScope(parent);
    365      MOZ_ASSERT(betterScope != Scope,
    366                 "How can we have the same scope for two different globals?");
    367      return GetNewOrUsed(cx, helper, betterScope, Interface, resultWrapper);
    368    }
    369 
    370    // Take the performance hit of checking the hashtable again in case
    371    // the preCreate call caused the wrapper to get created through some
    372    // interesting path (the DOM code tends to make this happen sometimes).
    373 
    374    if (cache) {
    375      RootedObject cached(cx, cache->GetWrapper());
    376      if (cached) {
    377        wrapper = XPCWrappedNative::Get(cached);
    378      }
    379    } else {
    380      wrapper = map->Find(identity);
    381    }
    382 
    383    if (wrapper) {
    384      if (!wrapper->FindTearOff(cx, Interface, false, &rv)) {
    385        MOZ_ASSERT(NS_FAILED(rv), "returning NS_OK on failure");
    386        return rv;
    387      }
    388      wrapper.forget(resultWrapper);
    389      return NS_OK;
    390    }
    391  } else {
    392    ar.emplace(static_cast<JSContext*>(cx), parent);
    393  }
    394 
    395  AutoMarkingWrappedNativeProtoPtr proto(cx);
    396 
    397  // If there is ClassInfo (and we are not building a wrapper for the
    398  // nsIClassInfo interface) then we use a wrapper that needs a prototype.
    399 
    400  // Note that the security check happens inside FindTearOff - after the
    401  // wrapper is actually created, but before JS code can see it.
    402 
    403  if (info && !isClassInfoSingleton) {
    404    proto = XPCWrappedNativeProto::GetNewOrUsed(cx, Scope, info, scrProto);
    405    if (!proto) {
    406      return NS_ERROR_FAILURE;
    407    }
    408 
    409    wrapper = new XPCWrappedNative(std::move(identity), proto);
    410  } else {
    411    RefPtr<XPCNativeInterface> iface = Interface;
    412    if (!iface) {
    413      iface = XPCNativeInterface::GetISupports(cx);
    414    }
    415 
    416    XPCNativeSetKey key(cx, iface);
    417    RefPtr<XPCNativeSet> set = XPCNativeSet::GetNewOrUsed(cx, &key);
    418 
    419    if (!set) {
    420      return NS_ERROR_FAILURE;
    421    }
    422 
    423    wrapper = new XPCWrappedNative(std::move(identity), Scope, set.forget());
    424  }
    425 
    426  MOZ_ASSERT(!xpc::WrapperFactory::IsXrayWrapper(parent),
    427             "Xray wrapper being used to parent XPCWrappedNative?");
    428 
    429  // We use an AutoMarkingPtr here because it is possible for JS gc to happen
    430  // after we have Init'd the wrapper but *before* we add it to the hashtable.
    431  // This would cause the mSet to get collected and we'd later crash. I've
    432  // *seen* this happen.
    433  AutoMarkingWrappedNativePtr wrapperMarker(cx, wrapper);
    434 
    435  if (!wrapper->Init(cx, scrWrapper)) {
    436    return NS_ERROR_FAILURE;
    437  }
    438 
    439  if (!wrapper->FindTearOff(cx, Interface, false, &rv)) {
    440    MOZ_ASSERT(NS_FAILED(rv), "returning NS_OK on failure");
    441    return rv;
    442  }
    443 
    444  return FinishCreate(cx, Scope, Interface, cache, wrapper, resultWrapper);
    445 }
    446 
    447 static nsresult FinishCreate(JSContext* cx, XPCWrappedNativeScope* Scope,
    448                             XPCNativeInterface* Interface,
    449                             nsWrapperCache* cache, XPCWrappedNative* inWrapper,
    450                             XPCWrappedNative** resultWrapper) {
    451  MOZ_ASSERT(inWrapper);
    452 
    453  Native2WrappedNativeMap* map = Scope->GetWrappedNativeMap();
    454 
    455  RefPtr<XPCWrappedNative> wrapper;
    456  // Deal with the case where the wrapper got created as a side effect
    457  // of one of our calls out of this code. Add() returns the (possibly
    458  // pre-existing) wrapper that ultimately ends up in the map, which is
    459  // what we want.
    460  wrapper = map->Add(inWrapper);
    461  if (!wrapper) {
    462    return NS_ERROR_FAILURE;
    463  }
    464 
    465  if (wrapper == inWrapper) {
    466    JSObject* flat = wrapper->GetFlatJSObject();
    467    MOZ_ASSERT(!cache || !cache->GetWrapperPreserveColor() ||
    468                   flat == cache->GetWrapperPreserveColor(),
    469               "This object has a cached wrapper that's different from "
    470               "the JSObject held by its native wrapper?");
    471 
    472    if (cache && !cache->GetWrapperPreserveColor()) {
    473      cache->SetWrapper(flat);
    474    }
    475  }
    476 
    477  DEBUG_CheckClassInfoClaims(wrapper);
    478  wrapper.forget(resultWrapper);
    479  return NS_OK;
    480 }
    481 
    482 // This ctor is used if this object will have a proto.
    483 XPCWrappedNative::XPCWrappedNative(nsCOMPtr<nsISupports>&& aIdentity,
    484                                   XPCWrappedNativeProto* aProto)
    485    : mMaybeProto(aProto), mSet(aProto->GetSet()) {
    486  MOZ_ASSERT(NS_IsMainThread());
    487 
    488  mIdentity = aIdentity;
    489  mFlatJSObject.setFlags(FLAT_JS_OBJECT_VALID);
    490 
    491  MOZ_ASSERT(mMaybeProto, "bad ctor param");
    492  MOZ_ASSERT(mSet, "bad ctor param");
    493 }
    494 
    495 // This ctor is used if this object will NOT have a proto.
    496 XPCWrappedNative::XPCWrappedNative(nsCOMPtr<nsISupports>&& aIdentity,
    497                                   XPCWrappedNativeScope* aScope,
    498                                   RefPtr<XPCNativeSet>&& aSet)
    499    : mMaybeScope(TagScope(aScope)), mSet(std::move(aSet)) {
    500  MOZ_ASSERT(NS_IsMainThread());
    501 
    502  mIdentity = aIdentity;
    503  mFlatJSObject.setFlags(FLAT_JS_OBJECT_VALID);
    504 
    505  MOZ_ASSERT(aScope, "bad ctor param");
    506  MOZ_ASSERT(mSet, "bad ctor param");
    507 }
    508 
    509 XPCWrappedNative::~XPCWrappedNative() { Destroy(); }
    510 
    511 void XPCWrappedNative::Destroy() {
    512  mScriptable = nullptr;
    513 
    514 #ifdef DEBUG
    515  // Check that this object has already been swept from the map.
    516  XPCWrappedNativeScope* scope = GetScope();
    517  if (scope) {
    518    Native2WrappedNativeMap* map = scope->GetWrappedNativeMap();
    519    MOZ_ASSERT(map->Find(GetIdentityObject()) != this);
    520  }
    521 #endif
    522 
    523  if (mIdentity) {
    524    XPCJSRuntime* rt = GetRuntime();
    525    if (rt && rt->GetDoingFinalization()) {
    526      DeferredFinalize(mIdentity.forget().take());
    527    } else {
    528      mIdentity = nullptr;
    529    }
    530  }
    531 
    532  mMaybeScope = nullptr;
    533 }
    534 
    535 // A hack for bug 517665, increase the probability for GC.
    536 // TODO: Try removing this and just using the actual size of the object.
    537 static const size_t GCMemoryFactor = 2;
    538 
    539 inline void XPCWrappedNative::SetFlatJSObject(JSObject* object) {
    540  MOZ_ASSERT(!mFlatJSObject);
    541  MOZ_ASSERT(object);
    542 
    543  JS::AddAssociatedMemory(object, sizeof(*this) * GCMemoryFactor,
    544                          JS::MemoryUse::XPCWrappedNative);
    545 
    546  mFlatJSObject = object;
    547  mFlatJSObject.setFlags(FLAT_JS_OBJECT_VALID);
    548 }
    549 
    550 inline void XPCWrappedNative::UnsetFlatJSObject() {
    551  MOZ_ASSERT(mFlatJSObject);
    552 
    553  JS::RemoveAssociatedMemory(mFlatJSObject.unbarrieredGetPtr(),
    554                             sizeof(*this) * GCMemoryFactor,
    555                             JS::MemoryUse::XPCWrappedNative);
    556 
    557  mFlatJSObject = nullptr;
    558  mFlatJSObject.unsetFlags(FLAT_JS_OBJECT_VALID);
    559 }
    560 
    561 // This is factored out so that it can be called publicly.
    562 // static
    563 nsIXPCScriptable* XPCWrappedNative::GatherProtoScriptable(
    564    nsIClassInfo* classInfo) {
    565  MOZ_ASSERT(classInfo, "bad param");
    566 
    567  nsCOMPtr<nsIXPCScriptable> helper;
    568  nsresult rv = classInfo->GetScriptableHelper(getter_AddRefs(helper));
    569  if (NS_SUCCEEDED(rv) && helper) {
    570    return helper;
    571  }
    572 
    573  return nullptr;
    574 }
    575 
    576 // static
    577 void XPCWrappedNative::GatherScriptable(nsISupports* aObj,
    578                                        nsIClassInfo* aClassInfo,
    579                                        nsIXPCScriptable** aScrProto,
    580                                        nsIXPCScriptable** aScrWrapper) {
    581  MOZ_ASSERT(!*aScrProto, "bad param");
    582  MOZ_ASSERT(!*aScrWrapper, "bad param");
    583 
    584  nsCOMPtr<nsIXPCScriptable> scrProto;
    585  nsCOMPtr<nsIXPCScriptable> scrWrapper;
    586 
    587  // Get the class scriptable helper (if present)
    588  if (aClassInfo) {
    589    scrProto = GatherProtoScriptable(aClassInfo);
    590  }
    591 
    592  // Do the same for the wrapper specific scriptable
    593  scrWrapper = do_QueryInterface(aObj);
    594  if (scrWrapper) {
    595    // A whole series of assertions to catch bad uses of scriptable flags on
    596    // the scrWrapper...
    597 
    598    // Can't set WANT_PRECREATE on an instance scriptable without also
    599    // setting it on the class scriptable.
    600    MOZ_ASSERT_IF(scrWrapper->WantPreCreate(),
    601                  scrProto && scrProto->WantPreCreate());
    602 
    603    // Can't set DONT_ENUM_QUERY_INTERFACE on an instance scriptable
    604    // without also setting it on the class scriptable (if present).
    605    MOZ_ASSERT_IF(scrWrapper->DontEnumQueryInterface() && scrProto,
    606                  scrProto->DontEnumQueryInterface());
    607 
    608    // Can't set ALLOW_PROP_MODS_DURING_RESOLVE on an instance scriptable
    609    // without also setting it on the class scriptable (if present).
    610    MOZ_ASSERT_IF(scrWrapper->AllowPropModsDuringResolve() && scrProto,
    611                  scrProto->AllowPropModsDuringResolve());
    612  } else {
    613    scrWrapper = scrProto;
    614  }
    615 
    616  scrProto.forget(aScrProto);
    617  scrWrapper.forget(aScrWrapper);
    618 }
    619 
    620 bool XPCWrappedNative::Init(JSContext* cx, nsIXPCScriptable* aScriptable) {
    621  // Setup our scriptable...
    622  MOZ_ASSERT(!mScriptable);
    623  mScriptable = aScriptable;
    624 
    625  // create our flatJSObject
    626 
    627  const JSClass* jsclazz =
    628      mScriptable ? mScriptable->GetJSClass() : &XPC_WN_NoHelper_JSClass;
    629 
    630  // We should have the global jsclass flag if and only if we're a global.
    631  MOZ_ASSERT_IF(mScriptable, !!mScriptable->IsGlobalObject() ==
    632                                 !!(jsclazz->flags & JSCLASS_IS_GLOBAL));
    633 
    634  MOZ_ASSERT(jsclazz && jsclazz->name && jsclazz->flags &&
    635                 jsclazz->getResolve() && jsclazz->hasFinalize(),
    636             "bad class");
    637 
    638  RootedObject protoJSObject(cx, HasProto() ? GetProto()->GetJSProtoObject()
    639                                            : JS::GetRealmObjectPrototype(cx));
    640  if (!protoJSObject) {
    641    return false;
    642  }
    643 
    644  JSObject* object = JS_NewObjectWithGivenProto(cx, jsclazz, protoJSObject);
    645  if (!object) {
    646    return false;
    647  }
    648 
    649  SetFlatJSObject(object);
    650 
    651  JS::SetObjectISupports(mFlatJSObject, this);
    652 
    653  return FinishInit(cx);
    654 }
    655 
    656 bool XPCWrappedNative::FinishInit(JSContext* cx) {
    657  // This reference will be released when mFlatJSObject is finalized.
    658  // Since this reference will push the refcount to 2 it will also root
    659  // mFlatJSObject;
    660  MOZ_ASSERT(1 == mRefCnt, "unexpected refcount value");
    661  NS_ADDREF(this);
    662 
    663  return true;
    664 }
    665 
    666 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(XPCWrappedNative)
    667  NS_INTERFACE_MAP_ENTRY(nsIXPConnectWrappedNative)
    668  NS_INTERFACE_MAP_ENTRY(nsIXPConnectJSObjectHolder)
    669  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIXPConnectWrappedNative)
    670 NS_INTERFACE_MAP_END
    671 
    672 NS_IMPL_CYCLE_COLLECTING_ADDREF(XPCWrappedNative)
    673 
    674 // Release calls Destroy() immediately when the refcount drops to 0 to
    675 // clear the weak references nsXPConnect has to XPCWNs and to ensure there
    676 // are no pointers to dying protos.
    677 NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_LAST_RELEASE(XPCWrappedNative, Destroy())
    678 
    679 /*
    680 *  Wrapped Native lifetime management is messy!
    681 *
    682 *  - At creation we push the refcount to 2 (only one of which is owned by
    683 *    the native caller that caused the wrapper creation).
    684 *  - During the JS GC Mark phase we mark any wrapper with a refcount > 1.
    685 *  - The *only* thing that can make the wrapper get destroyed is the
    686 *    finalization of mFlatJSObject. And *that* should only happen if the only
    687 *    reference is the single extra (internal) reference we hold.
    688 *
    689 *  - The wrapper has a pointer to the nsISupports 'view' of the wrapped native
    690 *    object i.e... mIdentity. This is held until the wrapper's refcount goes
    691 *    to zero and the wrapper is released, or until an expired wrapper (i.e.,
    692 *    one unlinked by the cycle collector) has had its JS object finalized.
    693 *
    694 *  - The wrapper also has 'tearoffs'. It has one tearoff for each interface
    695 *    that is actually used on the native object. 'Used' means we have either
    696 *    needed to QueryInterface to verify the availability of that interface
    697 *    of that we've had to QueryInterface in order to actually make a call
    698 *    into the wrapped object via the pointer for the given interface.
    699 *
    700 *  - Each tearoff's 'mNative' member (if non-null) indicates one reference
    701 *    held by our wrapper on the wrapped native for the given interface
    702 *    associated with the tearoff. If we release that reference then we set
    703 *    the tearoff's 'mNative' to null.
    704 *
    705 *  - We use the occasion of the JavaScript GCCallback for the JSGC_MARK_END
    706 *    event to scan the tearoffs of all wrappers for non-null mNative members
    707 *    that represent unused references. We can tell that a given tearoff's
    708 *    mNative is unused by noting that no live XPCCallContexts hold a pointer
    709 *    to the tearoff.
    710 *
    711 *  - As a time/space tradeoff we may decide to not do this scanning on
    712 *    *every* JavaScript GC. We *do* want to do this *sometimes* because
    713 *    we want to allow for wrapped native's to do their own tearoff patterns.
    714 *    So, we want to avoid holding references to interfaces that we don't need.
    715 *    At the same time, we don't want to be bracketing every call into a
    716 *    wrapped native object with a QueryInterface/Release pair. And we *never*
    717 *    make a call into the object except via the correct interface for which
    718 *    we've QI'd.
    719 *
    720 *  - Each tearoff *can* have a mJSObject whose lazily resolved properties
    721 *    represent the methods/attributes/constants of that specific interface.
    722 *    This is optionally reflected into JavaScript as "foo.nsIFoo" when "foo"
    723 *    is the name of mFlatJSObject and "nsIFoo" is the name of the given
    724 *    interface associated with the tearoff. When we create the tearoff's
    725 *    mJSObject we set it's parent to be mFlatJSObject. This way we know that
    726 *    when mFlatJSObject get's collected there are no outstanding reachable
    727 *    tearoff mJSObjects. Note that we must clear the private of any lingering
    728 *    mJSObjects at this point because we have no guarentee of the *order* of
    729 *    finalization within a given gc cycle.
    730 */
    731 
    732 void XPCWrappedNative::FlatJSObjectFinalized() {
    733  if (!IsValid()) {
    734    return;
    735  }
    736 
    737  // Iterate the tearoffs and null out each of their JSObject's privates.
    738  // This will keep them from trying to access their pointers to the
    739  // dying tearoff object. We can safely assume that those remaining
    740  // JSObjects are about to be finalized too.
    741 
    742  for (XPCWrappedNativeTearOff* to = &mFirstTearOff; to;
    743       to = to->GetNextTearOff()) {
    744    JSObject* jso = to->GetJSObjectPreserveColor();
    745    if (jso) {
    746      JS::SetReservedSlot(jso, XPCWrappedNativeTearOff::TearOffSlot,
    747                          JS::UndefinedValue());
    748      to->JSObjectFinalized();
    749    }
    750 
    751    // We also need to release any native pointers held...
    752    RefPtr<nsISupports> native = to->TakeNative();
    753    if (native && GetRuntime()) {
    754      DeferredFinalize(native.forget().take());
    755    }
    756 
    757    to->SetInterface(nullptr);
    758  }
    759 
    760  nsWrapperCache* cache = nullptr;
    761  CallQueryInterface(mIdentity, &cache);
    762  if (cache) {
    763    cache->ClearWrapper(mFlatJSObject.unbarrieredGetPtr());
    764  }
    765 
    766  UnsetFlatJSObject();
    767 
    768  MOZ_ASSERT(mIdentity, "bad pointer!");
    769 
    770  if (IsWrapperExpired()) {
    771    Destroy();
    772  }
    773 
    774  // Note that it's not safe to touch mNativeWrapper here since it's
    775  // likely that it has already been finalized.
    776 
    777  Release();
    778 }
    779 
    780 void XPCWrappedNative::FlatJSObjectMoved(JSObject* obj, const JSObject* old) {
    781  JS::AutoAssertGCCallback inCallback;
    782  MOZ_ASSERT(mFlatJSObject == old);
    783 
    784  nsWrapperCache* cache = nullptr;
    785  CallQueryInterface(mIdentity, &cache);
    786  if (cache) {
    787    cache->UpdateWrapper(obj, old);
    788  }
    789 
    790  mFlatJSObject = obj;
    791 }
    792 
    793 void XPCWrappedNative::SystemIsBeingShutDown() {
    794  if (!IsValid()) {
    795    return;
    796  }
    797 
    798  // The long standing strategy is to leak some objects still held at shutdown.
    799  // The general problem is that propagating release out of xpconnect at
    800  // shutdown time causes a world of problems.
    801 
    802  // We leak mIdentity (see above).
    803 
    804  // Short circuit future finalization.
    805  JS::SetObjectISupports(mFlatJSObject, nullptr);
    806  UnsetFlatJSObject();
    807 
    808  XPCWrappedNativeProto* proto = GetProto();
    809 
    810  if (HasProto()) {
    811    proto->SystemIsBeingShutDown();
    812  }
    813 
    814  // We don't clear mScriptable here. The destructor will do it.
    815 
    816  // Cleanup the tearoffs.
    817  for (XPCWrappedNativeTearOff* to = &mFirstTearOff; to;
    818       to = to->GetNextTearOff()) {
    819    if (JSObject* jso = to->GetJSObjectPreserveColor()) {
    820      JS::SetReservedSlot(jso, XPCWrappedNativeTearOff::TearOffSlot,
    821                          JS::UndefinedValue());
    822      to->SetJSObject(nullptr);
    823    }
    824    // We leak the tearoff mNative
    825    // (for the same reason we leak mIdentity - see above).
    826    (void)to->TakeNative().take();
    827    to->SetInterface(nullptr);
    828  }
    829 }
    830 
    831 /***************************************************************************/
    832 
    833 bool XPCWrappedNative::ExtendSet(JSContext* aCx,
    834                                 XPCNativeInterface* aInterface) {
    835  if (!mSet->HasInterface(aInterface)) {
    836    XPCNativeSetKey key(mSet, aInterface);
    837    RefPtr<XPCNativeSet> newSet = XPCNativeSet::GetNewOrUsed(aCx, &key);
    838    if (!newSet) {
    839      return false;
    840    }
    841 
    842    mSet = std::move(newSet);
    843  }
    844  return true;
    845 }
    846 
    847 XPCWrappedNativeTearOff* XPCWrappedNative::FindTearOff(
    848    JSContext* cx, XPCNativeInterface* aInterface,
    849    bool needJSObject /* = false */, nsresult* pError /* = nullptr */) {
    850  nsresult rv = NS_OK;
    851  XPCWrappedNativeTearOff* to;
    852  XPCWrappedNativeTearOff* firstAvailable = nullptr;
    853 
    854  XPCWrappedNativeTearOff* lastTearOff;
    855  for (lastTearOff = to = &mFirstTearOff; to;
    856       lastTearOff = to, to = to->GetNextTearOff()) {
    857    if (to->GetInterface() == aInterface) {
    858      if (needJSObject && !to->GetJSObjectPreserveColor()) {
    859        AutoMarkingWrappedNativeTearOffPtr tearoff(cx, to);
    860        bool ok = InitTearOffJSObject(cx, to);
    861        // During shutdown, we don't sweep tearoffs.  So make sure
    862        // to unmark manually in case the auto-marker marked us.
    863        // We shouldn't ever be getting here _during_ our
    864        // Mark/Sweep cycle, so this should be safe.
    865        to->Unmark();
    866        if (!ok) {
    867          to = nullptr;
    868          rv = NS_ERROR_OUT_OF_MEMORY;
    869        }
    870      }
    871      if (pError) {
    872        *pError = rv;
    873      }
    874      return to;
    875    }
    876    if (!firstAvailable && to->IsAvailable()) {
    877      firstAvailable = to;
    878    }
    879  }
    880 
    881  to = firstAvailable;
    882 
    883  if (!to) {
    884    to = lastTearOff->AddTearOff();
    885  }
    886 
    887  {
    888    // Scope keeps |tearoff| from leaking across the rest of the function.
    889    AutoMarkingWrappedNativeTearOffPtr tearoff(cx, to);
    890    rv = InitTearOff(cx, to, aInterface, needJSObject);
    891    // During shutdown, we don't sweep tearoffs.  So make sure to unmark
    892    // manually in case the auto-marker marked us.  We shouldn't ever be
    893    // getting here _during_ our Mark/Sweep cycle, so this should be safe.
    894    to->Unmark();
    895    if (NS_FAILED(rv)) {
    896      to = nullptr;
    897    }
    898  }
    899 
    900  if (pError) {
    901    *pError = rv;
    902  }
    903  return to;
    904 }
    905 
    906 XPCWrappedNativeTearOff* XPCWrappedNative::FindTearOff(JSContext* cx,
    907                                                       const nsIID& iid) {
    908  RefPtr<XPCNativeInterface> iface = XPCNativeInterface::GetNewOrUsed(cx, &iid);
    909  return iface ? FindTearOff(cx, iface) : nullptr;
    910 }
    911 
    912 nsresult XPCWrappedNative::InitTearOff(JSContext* cx,
    913                                       XPCWrappedNativeTearOff* aTearOff,
    914                                       XPCNativeInterface* aInterface,
    915                                       bool needJSObject) {
    916  // Determine if the object really does this interface...
    917 
    918  const nsIID* iid = aInterface->GetIID();
    919  nsISupports* identity = GetIdentityObject();
    920 
    921  // This is an nsRefPtr instead of an nsCOMPtr because it may not be the
    922  // canonical nsISupports for this object.
    923  RefPtr<nsISupports> qiResult;
    924 
    925  // We are about to call out to other code.
    926  // So protect our intended tearoff.
    927 
    928  aTearOff->SetReserved();
    929 
    930  if (NS_FAILED(identity->QueryInterface(*iid, getter_AddRefs(qiResult))) ||
    931      !qiResult) {
    932    aTearOff->SetInterface(nullptr);
    933    return NS_ERROR_NO_INTERFACE;
    934  }
    935 
    936  // Guard against trying to build a tearoff for a shared nsIClassInfo.
    937  if (iid->Equals(NS_GET_IID(nsIClassInfo))) {
    938    nsCOMPtr<nsISupports> alternate_identity(do_QueryInterface(qiResult));
    939    if (alternate_identity.get() != identity) {
    940      aTearOff->SetInterface(nullptr);
    941      return NS_ERROR_NO_INTERFACE;
    942    }
    943  }
    944 
    945  // Guard against trying to build a tearoff for an interface that is
    946  // aggregated and is implemented as a nsIXPConnectWrappedJS using this
    947  // self-same JSObject. The XBL system does this. If we mutate the set
    948  // of this wrapper then we will shadow the method that XBL has added to
    949  // the JSObject that it has inserted in the JS proto chain between our
    950  // JSObject and our XPCWrappedNativeProto's JSObject. If we let this
    951  // set mutation happen then the interface's methods will be added to
    952  // our JSObject, but calls on those methods will get routed up to
    953  // native code and into the wrappedJS - which will do a method lookup
    954  // on *our* JSObject and find the same method and make another call
    955  // into an infinite loop.
    956  // see: http://bugzilla.mozilla.org/show_bug.cgi?id=96725
    957 
    958  nsCOMPtr<nsIXPConnectWrappedJS> wrappedJS(do_QueryInterface(qiResult));
    959  if (wrappedJS) {
    960    RootedObject jso(cx, wrappedJS->GetJSObject());
    961    if (jso == mFlatJSObject) {
    962      // The implementing JSObject is the same as ours! Just say OK
    963      // without actually extending the set.
    964      //
    965      // XXX It is a little cheesy to have FindTearOff return an
    966      // 'empty' tearoff. But this is the centralized place to do the
    967      // QI activities on the underlying object. *And* most caller to
    968      // FindTearOff only look for a non-null result and ignore the
    969      // actual tearoff returned. The only callers that do use the
    970      // returned tearoff make sure to check for either a non-null
    971      // JSObject or a matching Interface before proceeding.
    972      // I think we can get away with this bit of ugliness.
    973 
    974      aTearOff->SetInterface(nullptr);
    975      return NS_OK;
    976    }
    977  }
    978 
    979  if (NS_FAILED(nsXPConnect::SecurityManager()->CanCreateWrapper(
    980          cx, *iid, identity, GetClassInfo()))) {
    981    // the security manager vetoed. It should have set an exception.
    982    aTearOff->SetInterface(nullptr);
    983    return NS_ERROR_XPC_SECURITY_MANAGER_VETO;
    984  }
    985 
    986  // If this is not already in our set we need to extend our set.
    987  // Note: we do not cache the result of the previous call to HasInterface()
    988  // because we unlocked and called out in the interim and the result of the
    989  // previous call might not be correct anymore.
    990 
    991  if (!mSet->HasInterface(aInterface) && !ExtendSet(cx, aInterface)) {
    992    aTearOff->SetInterface(nullptr);
    993    return NS_ERROR_NO_INTERFACE;
    994  }
    995 
    996  aTearOff->SetInterface(aInterface);
    997  aTearOff->SetNative(qiResult);
    998 
    999  if (needJSObject && !InitTearOffJSObject(cx, aTearOff)) {
   1000    return NS_ERROR_OUT_OF_MEMORY;
   1001  }
   1002 
   1003  return NS_OK;
   1004 }
   1005 
   1006 bool XPCWrappedNative::InitTearOffJSObject(JSContext* cx,
   1007                                           XPCWrappedNativeTearOff* to) {
   1008  JSObject* obj = JS_NewObject(cx, &XPC_WN_Tearoff_JSClass);
   1009  if (!obj) {
   1010    return false;
   1011  }
   1012 
   1013  JS::SetReservedSlot(obj, XPCWrappedNativeTearOff::TearOffSlot,
   1014                      JS::PrivateValue(to));
   1015  to->SetJSObject(obj);
   1016 
   1017  JS::SetReservedSlot(obj, XPCWrappedNativeTearOff::FlatObjectSlot,
   1018                      JS::ObjectValue(*mFlatJSObject));
   1019  return true;
   1020 }
   1021 
   1022 /***************************************************************************/
   1023 
   1024 static bool Throw(nsresult errNum, XPCCallContext& ccx) {
   1025  XPCThrower::Throw(errNum, ccx);
   1026  return false;
   1027 }
   1028 
   1029 /***************************************************************************/
   1030 
   1031 class MOZ_STACK_CLASS CallMethodHelper final {
   1032  XPCCallContext& mCallContext;
   1033  nsresult mInvokeResult;
   1034  const nsXPTInterfaceInfo* const mIFaceInfo;
   1035  const nsXPTMethodInfo* mMethodInfo;
   1036  nsISupports* const mCallee;
   1037  const uint16_t mVTableIndex;
   1038  HandleId mIdxValueId;
   1039 
   1040  AutoTArray<nsXPTCVariant, 8> mDispatchParams;
   1041  uint8_t mJSContextIndex;  // TODO make const
   1042  uint8_t mOptArgcIndex;    // TODO make const
   1043 
   1044  Value* const mArgv;
   1045  const uint32_t mArgc;
   1046 
   1047  MOZ_ALWAYS_INLINE bool GetArraySizeFromParam(const nsXPTType& type,
   1048                                               HandleValue maybeArray,
   1049                                               uint32_t* result);
   1050 
   1051  MOZ_ALWAYS_INLINE bool GetInterfaceTypeFromParam(const nsXPTType& type,
   1052                                                   nsID* result) const;
   1053 
   1054  MOZ_ALWAYS_INLINE bool GetOutParamSource(uint8_t paramIndex,
   1055                                           MutableHandleValue srcp) const;
   1056 
   1057  MOZ_ALWAYS_INLINE bool GatherAndConvertResults();
   1058 
   1059  MOZ_ALWAYS_INLINE bool QueryInterfaceFastPath();
   1060 
   1061  nsXPTCVariant* GetDispatchParam(uint8_t paramIndex) {
   1062    if (paramIndex >= mJSContextIndex) {
   1063      paramIndex += 1;
   1064    }
   1065    if (paramIndex >= mOptArgcIndex) {
   1066      paramIndex += 1;
   1067    }
   1068    return &mDispatchParams[paramIndex];
   1069  }
   1070  const nsXPTCVariant* GetDispatchParam(uint8_t paramIndex) const {
   1071    return const_cast<CallMethodHelper*>(this)->GetDispatchParam(paramIndex);
   1072  }
   1073 
   1074  MOZ_ALWAYS_INLINE bool InitializeDispatchParams();
   1075 
   1076  MOZ_ALWAYS_INLINE bool ConvertIndependentParams(bool* foundDependentParam);
   1077  MOZ_ALWAYS_INLINE bool ConvertIndependentParam(uint8_t i);
   1078  MOZ_ALWAYS_INLINE bool ConvertDependentParams();
   1079  MOZ_ALWAYS_INLINE bool ConvertDependentParam(uint8_t i);
   1080 
   1081  MOZ_ALWAYS_INLINE nsresult Invoke();
   1082 
   1083 public:
   1084  explicit CallMethodHelper(XPCCallContext& ccx)
   1085      : mCallContext(ccx),
   1086        mInvokeResult(NS_ERROR_UNEXPECTED),
   1087        mIFaceInfo(ccx.GetInterface()->GetInterfaceInfo()),
   1088        mMethodInfo(nullptr),
   1089        mCallee(ccx.GetTearOff()->GetNative()),
   1090        mVTableIndex(ccx.GetMethodIndex()),
   1091        mIdxValueId(ccx.GetContext()->GetStringID(XPCJSContext::IDX_VALUE)),
   1092        mJSContextIndex(UINT8_MAX),
   1093        mOptArgcIndex(UINT8_MAX),
   1094        mArgv(ccx.GetArgv()),
   1095        mArgc(ccx.GetArgc())
   1096 
   1097  {
   1098    // Success checked later.
   1099    mIFaceInfo->GetMethodInfo(mVTableIndex, &mMethodInfo);
   1100  }
   1101 
   1102  ~CallMethodHelper();
   1103 
   1104  MOZ_ALWAYS_INLINE bool Call();
   1105 
   1106  // Trace implementation so we can put our CallMethodHelper in a Rooted<T>.
   1107  void trace(JSTracer* aTrc);
   1108 };
   1109 
   1110 // static
   1111 bool XPCWrappedNative::CallMethod(XPCCallContext& ccx,
   1112                                  CallMode mode /*= CALL_METHOD */) {
   1113  nsresult rv = ccx.CanCallNow();
   1114  if (NS_FAILED(rv)) {
   1115    return Throw(rv, ccx);
   1116  }
   1117 
   1118  JS::Rooted<CallMethodHelper> helper(ccx, /* init = */ ccx);
   1119  return helper.get().Call();
   1120 }
   1121 
   1122 bool CallMethodHelper::Call() {
   1123  mCallContext.SetRetVal(JS::UndefinedValue());
   1124 
   1125  mCallContext.GetContext()->SetPendingException(nullptr);
   1126 
   1127  using Flags = js::ProfilingStackFrame::Flags;
   1128  if (mVTableIndex == 0) {
   1129    AUTO_PROFILER_LABEL_DYNAMIC_FAST(mIFaceInfo->Name(), "QueryInterface", DOM,
   1130                                     mCallContext.GetJSContext(),
   1131                                     uint32_t(Flags::STRING_TEMPLATE_METHOD) |
   1132                                         uint32_t(Flags::RELEVANT_FOR_JS));
   1133 
   1134    return QueryInterfaceFastPath();
   1135  }
   1136 
   1137  if (!mMethodInfo) {
   1138    Throw(NS_ERROR_XPC_CANT_GET_METHOD_INFO, mCallContext);
   1139    return false;
   1140  }
   1141 
   1142  // Add profiler labels matching the WebIDL profiler labels,
   1143  // which also use the DOM category.
   1144  Flags templateFlag = Flags::STRING_TEMPLATE_METHOD;
   1145  if (mMethodInfo->IsGetter()) {
   1146    templateFlag = Flags::STRING_TEMPLATE_GETTER;
   1147  }
   1148  if (mMethodInfo->IsSetter()) {
   1149    templateFlag = Flags::STRING_TEMPLATE_SETTER;
   1150  }
   1151  AUTO_PROFILER_LABEL_DYNAMIC_FAST(
   1152      mIFaceInfo->Name(), mMethodInfo->NameOrDescription(), DOM,
   1153      mCallContext.GetJSContext(),
   1154      uint32_t(templateFlag) | uint32_t(Flags::RELEVANT_FOR_JS));
   1155 
   1156  if (!InitializeDispatchParams()) {
   1157    return false;
   1158  }
   1159 
   1160  // Iterate through the params doing conversions of independent params only.
   1161  // When we later convert the dependent params (if any) we will know that
   1162  // the params upon which they depend will have already been converted -
   1163  // regardless of ordering.
   1164  bool foundDependentParam = false;
   1165  if (!ConvertIndependentParams(&foundDependentParam)) {
   1166    return false;
   1167  }
   1168 
   1169  if (foundDependentParam && !ConvertDependentParams()) {
   1170    return false;
   1171  }
   1172 
   1173  mInvokeResult = Invoke();
   1174 
   1175  if (JS_IsExceptionPending(mCallContext)) {
   1176    return false;
   1177  }
   1178 
   1179  if (NS_FAILED(mInvokeResult)) {
   1180    ThrowBadResult(mInvokeResult, mCallContext);
   1181    return false;
   1182  }
   1183 
   1184  return GatherAndConvertResults();
   1185 }
   1186 
   1187 CallMethodHelper::~CallMethodHelper() {
   1188  for (nsXPTCVariant& param : mDispatchParams) {
   1189    uint32_t arraylen = 0;
   1190    if (!GetArraySizeFromParam(param.type, UndefinedHandleValue, &arraylen)) {
   1191      continue;
   1192    }
   1193 
   1194    xpc::DestructValue(param.type, &param.val, arraylen);
   1195  }
   1196 }
   1197 
   1198 bool CallMethodHelper::GetArraySizeFromParam(const nsXPTType& type,
   1199                                             HandleValue maybeArray,
   1200                                             uint32_t* result) {
   1201  if (type.Tag() != nsXPTType::T_LEGACY_ARRAY &&
   1202      type.Tag() != nsXPTType::T_PSTRING_SIZE_IS &&
   1203      type.Tag() != nsXPTType::T_PWSTRING_SIZE_IS) {
   1204    *result = 0;
   1205    return true;
   1206  }
   1207 
   1208  uint8_t argnum = type.ArgNum();
   1209  uint32_t* lengthp = &GetDispatchParam(argnum)->val.u32;
   1210 
   1211  // TODO fixup the various exceptions that are thrown
   1212 
   1213  // If the array length wasn't passed, it might have been listed as optional.
   1214  // When converting arguments from JS to C++, we pass the array as
   1215  // |maybeArray|, and give ourselves the chance to infer the length. Once we
   1216  // have it, we stick it in the right slot so that we can find it again when
   1217  // cleaning up the params. from the array.
   1218  if (argnum >= mArgc && maybeArray.isObject()) {
   1219    MOZ_ASSERT(mMethodInfo->Param(argnum).IsOptional());
   1220    RootedObject arrayOrNull(mCallContext, &maybeArray.toObject());
   1221 
   1222    bool isArray;
   1223    bool ok = false;
   1224    if (JS::IsArrayObject(mCallContext, maybeArray, &isArray) && isArray) {
   1225      ok = JS::GetArrayLength(mCallContext, arrayOrNull, lengthp);
   1226    } else if (JS_IsTypedArrayObject(&maybeArray.toObject())) {
   1227      size_t len = JS_GetTypedArrayLength(&maybeArray.toObject());
   1228      if (len <= UINT32_MAX) {
   1229        *lengthp = len;
   1230        ok = true;
   1231      }
   1232    }
   1233 
   1234    if (!ok) {
   1235      return Throw(NS_ERROR_XPC_CANT_CONVERT_OBJECT_TO_ARRAY, mCallContext);
   1236    }
   1237  }
   1238 
   1239  *result = *lengthp;
   1240  return true;
   1241 }
   1242 
   1243 bool CallMethodHelper::GetInterfaceTypeFromParam(const nsXPTType& type,
   1244                                                 nsID* result) const {
   1245  result->Clear();
   1246 
   1247  const nsXPTType& inner = type.InnermostType();
   1248  if (inner.Tag() == nsXPTType::T_INTERFACE) {
   1249    if (!inner.GetInterface()) {
   1250      return Throw(NS_ERROR_XPC_CANT_GET_PARAM_IFACE_INFO, mCallContext);
   1251    }
   1252 
   1253    *result = inner.GetInterface()->IID();
   1254  } else if (inner.Tag() == nsXPTType::T_INTERFACE_IS) {
   1255    const nsXPTCVariant* param = GetDispatchParam(inner.ArgNum());
   1256    if (param->type.Tag() != nsXPTType::T_NSID &&
   1257        param->type.Tag() != nsXPTType::T_NSIDPTR) {
   1258      return Throw(NS_ERROR_UNEXPECTED, mCallContext);
   1259    }
   1260 
   1261    const void* ptr = &param->val;
   1262    if (param->type.Tag() == nsXPTType::T_NSIDPTR) {
   1263      ptr = *static_cast<nsID* const*>(ptr);
   1264    }
   1265 
   1266    if (!ptr) {
   1267      return ThrowBadParam(NS_ERROR_XPC_CANT_GET_PARAM_IFACE_INFO,
   1268                           inner.ArgNum(), mCallContext);
   1269    }
   1270 
   1271    *result = *static_cast<const nsID*>(ptr);
   1272  }
   1273  return true;
   1274 }
   1275 
   1276 bool CallMethodHelper::GetOutParamSource(uint8_t paramIndex,
   1277                                         MutableHandleValue srcp) const {
   1278  const nsXPTParamInfo& paramInfo = mMethodInfo->Param(paramIndex);
   1279  bool isRetval = &paramInfo == mMethodInfo->GetRetval();
   1280 
   1281  if (paramInfo.IsOut() && !isRetval) {
   1282    MOZ_ASSERT(paramIndex < mArgc || paramInfo.IsOptional(),
   1283               "Expected either enough arguments or an optional argument");
   1284    Value arg = paramIndex < mArgc ? mArgv[paramIndex] : JS::NullValue();
   1285    if (paramIndex < mArgc) {
   1286      RootedObject obj(mCallContext);
   1287      if (!arg.isPrimitive()) {
   1288        obj = &arg.toObject();
   1289      }
   1290      if (!obj || !JS_GetPropertyById(mCallContext, obj, mIdxValueId, srcp)) {
   1291        // Explicitly passed in unusable value for out param.  Note
   1292        // that if i >= mArgc we already know that |arg| is JS::NullValue(),
   1293        // and that's ok.
   1294        ThrowBadParam(NS_ERROR_XPC_NEED_OUT_OBJECT, paramIndex, mCallContext);
   1295        return false;
   1296      }
   1297    }
   1298  }
   1299 
   1300  return true;
   1301 }
   1302 
   1303 bool CallMethodHelper::GatherAndConvertResults() {
   1304  // now we iterate through the native params to gather and convert results
   1305  uint8_t paramCount = mMethodInfo->ParamCount();
   1306  for (uint8_t i = 0; i < paramCount; i++) {
   1307    const nsXPTParamInfo& paramInfo = mMethodInfo->Param(i);
   1308    if (!paramInfo.IsOut()) {
   1309      continue;
   1310    }
   1311 
   1312    const nsXPTType& type = paramInfo.GetType();
   1313    nsXPTCVariant* dp = GetDispatchParam(i);
   1314    RootedValue v(mCallContext, NullValue());
   1315 
   1316    uint32_t array_count = 0;
   1317    nsID param_iid;
   1318    if (!GetInterfaceTypeFromParam(type, &param_iid) ||
   1319        !GetArraySizeFromParam(type, UndefinedHandleValue, &array_count))
   1320      return false;
   1321 
   1322    nsresult err;
   1323    if (!XPCConvert::NativeData2JS(mCallContext, &v, &dp->val, type, &param_iid,
   1324                                   array_count, &err)) {
   1325      ThrowBadParam(err, i, mCallContext);
   1326      return false;
   1327    }
   1328 
   1329    if (&paramInfo == mMethodInfo->GetRetval()) {
   1330      mCallContext.SetRetVal(v);
   1331    } else if (i < mArgc) {
   1332      // we actually assured this before doing the invoke
   1333      MOZ_ASSERT(mArgv[i].isObject(), "out var is not object");
   1334      RootedObject obj(mCallContext, &mArgv[i].toObject());
   1335      if (!JS_SetPropertyById(mCallContext, obj, mIdxValueId, v)) {
   1336        ThrowBadParam(NS_ERROR_XPC_CANT_SET_OUT_VAL, i, mCallContext);
   1337        return false;
   1338      }
   1339    } else {
   1340      MOZ_ASSERT(paramInfo.IsOptional(),
   1341                 "Expected either enough arguments or an optional argument");
   1342    }
   1343  }
   1344 
   1345  return true;
   1346 }
   1347 
   1348 bool CallMethodHelper::QueryInterfaceFastPath() {
   1349  MOZ_ASSERT(mVTableIndex == 0,
   1350             "Using the QI fast-path for a method other than QueryInterface");
   1351 
   1352  if (mArgc < 1) {
   1353    Throw(NS_ERROR_XPC_NOT_ENOUGH_ARGS, mCallContext);
   1354    return false;
   1355  }
   1356 
   1357  if (!mArgv[0].isObject()) {
   1358    ThrowBadParam(NS_ERROR_XPC_BAD_CONVERT_JS, 0, mCallContext);
   1359    return false;
   1360  }
   1361 
   1362  JS::RootedValue iidarg(mCallContext, mArgv[0]);
   1363  Maybe<nsID> iid = xpc::JSValue2ID(mCallContext, iidarg);
   1364  if (!iid) {
   1365    ThrowBadParam(NS_ERROR_XPC_BAD_CONVERT_JS, 0, mCallContext);
   1366    return false;
   1367  }
   1368 
   1369  nsISupports* qiresult = nullptr;
   1370  mInvokeResult = mCallee->QueryInterface(iid.ref(), (void**)&qiresult);
   1371 
   1372  if (NS_FAILED(mInvokeResult)) {
   1373    ThrowBadResult(mInvokeResult, mCallContext);
   1374    return false;
   1375  }
   1376 
   1377  RootedValue v(mCallContext, NullValue());
   1378  nsresult err;
   1379  bool success = XPCConvert::NativeData2JS(mCallContext, &v, &qiresult,
   1380                                           {nsXPTType::T_INTERFACE_IS},
   1381                                           iid.ptr(), 0, &err);
   1382  NS_IF_RELEASE(qiresult);
   1383 
   1384  if (!success) {
   1385    ThrowBadParam(err, 0, mCallContext);
   1386    return false;
   1387  }
   1388 
   1389  mCallContext.SetRetVal(v);
   1390  return true;
   1391 }
   1392 
   1393 bool CallMethodHelper::InitializeDispatchParams() {
   1394  const uint8_t wantsOptArgc = mMethodInfo->WantsOptArgc() ? 1 : 0;
   1395  const uint8_t wantsJSContext = mMethodInfo->WantsContext() ? 1 : 0;
   1396  const uint8_t paramCount = mMethodInfo->ParamCount();
   1397  uint8_t requiredArgs = paramCount;
   1398 
   1399  // XXX ASSUMES that retval is last arg. The xpidl compiler ensures this.
   1400  if (mMethodInfo->HasRetval()) {
   1401    requiredArgs--;
   1402  }
   1403 
   1404  if (mArgc < requiredArgs || wantsOptArgc) {
   1405    if (wantsOptArgc) {
   1406      // The implicit JSContext*, if we have one, comes first.
   1407      mOptArgcIndex = requiredArgs + wantsJSContext;
   1408    }
   1409 
   1410    // skip over any optional arguments
   1411    while (requiredArgs && mMethodInfo->Param(requiredArgs - 1).IsOptional()) {
   1412      requiredArgs--;
   1413    }
   1414 
   1415    if (mArgc < requiredArgs) {
   1416      Throw(NS_ERROR_XPC_NOT_ENOUGH_ARGS, mCallContext);
   1417      return false;
   1418    }
   1419  }
   1420 
   1421  mJSContextIndex = mMethodInfo->IndexOfJSContext();
   1422 
   1423  // Allocate enough space in mDispatchParams up-front.
   1424  // XXX(Bug 1631371) Check if this should use a fallible operation as it
   1425  // pretended earlier.
   1426  mDispatchParams.AppendElements(paramCount + wantsJSContext + wantsOptArgc);
   1427 
   1428  // Initialize each parameter to a valid state (for safe cleanup later).
   1429  for (uint8_t i = 0, paramIdx = 0; i < mDispatchParams.Length(); i++) {
   1430    nsXPTCVariant& dp = mDispatchParams[i];
   1431 
   1432    if (i == mJSContextIndex) {
   1433      // Fill in the JSContext argument
   1434      dp.type = nsXPTType::T_VOID;
   1435      dp.val.p = mCallContext;
   1436    } else if (i == mOptArgcIndex) {
   1437      // Fill in the optional_argc argument
   1438      dp.type = nsXPTType::T_U8;
   1439      dp.val.u8 = std::min<uint32_t>(mArgc, paramCount) - requiredArgs;
   1440    } else {
   1441      // Initialize normal arguments.
   1442      const nsXPTParamInfo& param = mMethodInfo->Param(paramIdx);
   1443      dp.type = param.Type();
   1444      xpc::InitializeValue(dp.type, &dp.val);
   1445 
   1446      // Specify the correct storage/calling semantics. This will also set
   1447      // the `ptr` field to be self-referential.
   1448      if (param.IsIndirect()) {
   1449        dp.SetIndirect();
   1450      }
   1451 
   1452      // Advance to the next normal parameter.
   1453      paramIdx++;
   1454    }
   1455  }
   1456 
   1457  return true;
   1458 }
   1459 
   1460 bool CallMethodHelper::ConvertIndependentParams(bool* foundDependentParam) {
   1461  const uint8_t paramCount = mMethodInfo->ParamCount();
   1462  for (uint8_t i = 0; i < paramCount; i++) {
   1463    const nsXPTParamInfo& paramInfo = mMethodInfo->Param(i);
   1464 
   1465    if (paramInfo.GetType().IsDependent()) {
   1466      *foundDependentParam = true;
   1467    } else if (!ConvertIndependentParam(i)) {
   1468      return false;
   1469    }
   1470  }
   1471 
   1472  return true;
   1473 }
   1474 
   1475 bool CallMethodHelper::ConvertIndependentParam(uint8_t i) {
   1476  const nsXPTParamInfo& paramInfo = mMethodInfo->Param(i);
   1477  const nsXPTType& type = paramInfo.Type();
   1478  nsXPTCVariant* dp = GetDispatchParam(i);
   1479 
   1480  // Even if there's nothing to convert, we still need to examine the
   1481  // JSObject container for out-params. If it's null or otherwise invalid,
   1482  // we want to know before the call, rather than after.
   1483  //
   1484  // This is a no-op for 'in' params.
   1485  RootedValue src(mCallContext);
   1486  if (!GetOutParamSource(i, &src)) {
   1487    return false;
   1488  }
   1489 
   1490  // All that's left to do is value conversion. Bail early if we don't need
   1491  // to do that.
   1492  if (!paramInfo.IsIn()) {
   1493    return true;
   1494  }
   1495 
   1496  // Some types usually don't support default values, but we want to handle
   1497  // the default value if IsOptional is true.
   1498  if (i >= mArgc) {
   1499    MOZ_ASSERT(paramInfo.IsOptional(), "missing non-optional argument!");
   1500    if (type.Tag() == nsXPTType::T_NSID) {
   1501      // Use a default value of the null ID for optional NSID objects.
   1502      dp->ext.nsid.Clear();
   1503      return true;
   1504    }
   1505 
   1506    if (type.Tag() == nsXPTType::T_ARRAY) {
   1507      // Use a default value of empty array for optional Array objects.
   1508      dp->ext.array.Clear();
   1509      return true;
   1510    }
   1511  }
   1512 
   1513  // We're definitely some variety of 'in' now, so there's something to
   1514  // convert. The source value for conversion depends on whether we're
   1515  // dealing with an 'in' or an 'inout' parameter. 'inout' was handled above,
   1516  // so all that's left is 'in'.
   1517  if (!paramInfo.IsOut()) {
   1518    // Handle the 'in' case.
   1519    MOZ_ASSERT(i < mArgc || paramInfo.IsOptional(),
   1520               "Expected either enough arguments or an optional argument");
   1521    if (i < mArgc) {
   1522      src = mArgv[i];
   1523    } else if (type.Tag() == nsXPTType::T_JSVAL) {
   1524      src.setUndefined();
   1525    } else {
   1526      src.setNull();
   1527    }
   1528  }
   1529 
   1530  nsID param_iid = {0};
   1531  const nsXPTType& inner = type.InnermostType();
   1532  if (inner.Tag() == nsXPTType::T_INTERFACE) {
   1533    if (!inner.GetInterface()) {
   1534      return ThrowBadParam(NS_ERROR_XPC_CANT_GET_PARAM_IFACE_INFO, i,
   1535                           mCallContext);
   1536    }
   1537    param_iid = inner.GetInterface()->IID();
   1538  }
   1539 
   1540  nsresult err;
   1541  if (!XPCConvert::JSData2Native(mCallContext, &dp->val, src, type, &param_iid,
   1542                                 0, &err)) {
   1543    ThrowBadParam(err, i, mCallContext);
   1544    return false;
   1545  }
   1546 
   1547  return true;
   1548 }
   1549 
   1550 bool CallMethodHelper::ConvertDependentParams() {
   1551  const uint8_t paramCount = mMethodInfo->ParamCount();
   1552  for (uint8_t i = 0; i < paramCount; i++) {
   1553    const nsXPTParamInfo& paramInfo = mMethodInfo->Param(i);
   1554 
   1555    if (!paramInfo.GetType().IsDependent()) {
   1556      continue;
   1557    }
   1558    if (!ConvertDependentParam(i)) {
   1559      return false;
   1560    }
   1561  }
   1562 
   1563  return true;
   1564 }
   1565 
   1566 bool CallMethodHelper::ConvertDependentParam(uint8_t i) {
   1567  const nsXPTParamInfo& paramInfo = mMethodInfo->Param(i);
   1568  const nsXPTType& type = paramInfo.Type();
   1569  nsXPTCVariant* dp = GetDispatchParam(i);
   1570 
   1571  // Even if there's nothing to convert, we still need to examine the
   1572  // JSObject container for out-params. If it's null or otherwise invalid,
   1573  // we want to know before the call, rather than after.
   1574  //
   1575  // This is a no-op for 'in' params.
   1576  RootedValue src(mCallContext);
   1577  if (!GetOutParamSource(i, &src)) {
   1578    return false;
   1579  }
   1580 
   1581  // All that's left to do is value conversion. Bail early if we don't need
   1582  // to do that.
   1583  if (!paramInfo.IsIn()) {
   1584    return true;
   1585  }
   1586 
   1587  // We're definitely some variety of 'in' now, so there's something to
   1588  // convert. The source value for conversion depends on whether we're
   1589  // dealing with an 'in' or an 'inout' parameter. 'inout' was handled above,
   1590  // so all that's left is 'in'.
   1591  if (!paramInfo.IsOut()) {
   1592    // Handle the 'in' case.
   1593    MOZ_ASSERT(i < mArgc || paramInfo.IsOptional(),
   1594               "Expected either enough arguments or an optional argument");
   1595    src = i < mArgc ? mArgv[i] : JS::NullValue();
   1596  }
   1597 
   1598  nsID param_iid;
   1599  uint32_t array_count;
   1600  if (!GetInterfaceTypeFromParam(type, &param_iid) ||
   1601      !GetArraySizeFromParam(type, src, &array_count))
   1602    return false;
   1603 
   1604  nsresult err;
   1605 
   1606  if (!XPCConvert::JSData2Native(mCallContext, &dp->val, src, type, &param_iid,
   1607                                 array_count, &err)) {
   1608    ThrowBadParam(err, i, mCallContext);
   1609    return false;
   1610  }
   1611 
   1612  return true;
   1613 }
   1614 
   1615 nsresult CallMethodHelper::Invoke() {
   1616  uint32_t argc = mDispatchParams.Length();
   1617  nsXPTCVariant* argv = mDispatchParams.Elements();
   1618 
   1619  return NS_InvokeByIndex(mCallee, mVTableIndex, argc, argv);
   1620 }
   1621 
   1622 static void TraceParam(JSTracer* aTrc, void* aVal, const nsXPTType& aType,
   1623                       uint32_t aArrayLen = 0) {
   1624  if (aType.Tag() == nsXPTType::T_JSVAL) {
   1625    JS::TraceRoot(aTrc, (JS::Value*)aVal, "XPCWrappedNative::CallMethod param");
   1626  } else if (aType.Tag() == nsXPTType::T_ARRAY) {
   1627    auto* array = (xpt::detail::UntypedTArray*)aVal;
   1628    const nsXPTType& elty = aType.ArrayElementType();
   1629 
   1630    for (uint32_t i = 0; i < array->Length(); ++i) {
   1631      TraceParam(aTrc, elty.ElementPtr(array->Elements(), i), elty);
   1632    }
   1633  } else if (aType.Tag() == nsXPTType::T_LEGACY_ARRAY && *(void**)aVal) {
   1634    const nsXPTType& elty = aType.ArrayElementType();
   1635 
   1636    for (uint32_t i = 0; i < aArrayLen; ++i) {
   1637      TraceParam(aTrc, elty.ElementPtr(*(void**)aVal, i), elty);
   1638    }
   1639  }
   1640 }
   1641 
   1642 void CallMethodHelper::trace(JSTracer* aTrc) {
   1643  // We need to note each of our initialized parameters which contain jsvals.
   1644  for (nsXPTCVariant& param : mDispatchParams) {
   1645    // We only need to trace parameters which have an innermost JSVAL.
   1646    if (param.type.InnermostType().Tag() != nsXPTType::T_JSVAL) {
   1647      continue;
   1648    }
   1649 
   1650    uint32_t arrayLen = 0;
   1651    if (!GetArraySizeFromParam(param.type, UndefinedHandleValue, &arrayLen)) {
   1652      continue;
   1653    }
   1654 
   1655    TraceParam(aTrc, &param.val, param.type, arrayLen);
   1656  }
   1657 }
   1658 
   1659 /***************************************************************************/
   1660 // interface methods
   1661 
   1662 JSObject* XPCWrappedNative::GetJSObject() { return GetFlatJSObject(); }
   1663 
   1664 XPCWrappedNative* nsIXPConnectWrappedNative::AsXPCWrappedNative() {
   1665  return static_cast<XPCWrappedNative*>(this);
   1666 }
   1667 
   1668 nsresult nsIXPConnectWrappedNative::DebugDump(int16_t depth) {
   1669  return AsXPCWrappedNative()->DebugDump(depth);
   1670 }
   1671 
   1672 nsresult XPCWrappedNative::DebugDump(int16_t depth) {
   1673 #ifdef DEBUG
   1674  depth--;
   1675  XPC_LOG_ALWAYS(
   1676      ("XPCWrappedNative @ %p with mRefCnt = %" PRIuPTR, this, mRefCnt.get()));
   1677  XPC_LOG_INDENT();
   1678 
   1679  if (HasProto()) {
   1680    XPCWrappedNativeProto* proto = GetProto();
   1681    if (depth && proto) {
   1682      proto->DebugDump(depth);
   1683    } else {
   1684      XPC_LOG_ALWAYS(("mMaybeProto @ %p", proto));
   1685    }
   1686  } else
   1687    XPC_LOG_ALWAYS(("Scope @ %p", GetScope()));
   1688 
   1689  if (depth && mSet) {
   1690    mSet->DebugDump(depth);
   1691  } else {
   1692    XPC_LOG_ALWAYS(("mSet @ %p", mSet.get()));
   1693  }
   1694 
   1695  XPC_LOG_ALWAYS(("mFlatJSObject of %p", mFlatJSObject.unbarrieredGetPtr()));
   1696  XPC_LOG_ALWAYS(("mIdentity of %p", mIdentity.get()));
   1697  XPC_LOG_ALWAYS(("mScriptable @ %p", mScriptable.get()));
   1698 
   1699  if (depth && mScriptable) {
   1700    XPC_LOG_INDENT();
   1701    XPC_LOG_ALWAYS(("mFlags of %x", mScriptable->GetScriptableFlags()));
   1702    XPC_LOG_ALWAYS(("mJSClass @ %p", mScriptable->GetJSClass()));
   1703    XPC_LOG_OUTDENT();
   1704  }
   1705  XPC_LOG_OUTDENT();
   1706 #endif
   1707  return NS_OK;
   1708 }
   1709 
   1710 /***************************************************************************/
   1711 
   1712 char* XPCWrappedNative::ToString(
   1713    XPCWrappedNativeTearOff* to /* = nullptr */) const {
   1714 #ifdef DEBUG
   1715 #  define FMT_ADDR " @ 0x%p"
   1716 #  define FMT_STR(str) str
   1717 #  define PARAM_ADDR(w) , w
   1718 #else
   1719 #  define FMT_ADDR ""
   1720 #  define FMT_STR(str)
   1721 #  define PARAM_ADDR(w)
   1722 #endif
   1723 
   1724  UniqueChars sz;
   1725  UniqueChars name;
   1726 
   1727  nsCOMPtr<nsIXPCScriptable> scr = GetScriptable();
   1728  if (scr) {
   1729    name = JS_smprintf("%s", scr->GetJSClass()->name);
   1730  }
   1731  if (to) {
   1732    const char* fmt = name ? " (%s)" : "%s";
   1733    name = JS_sprintf_append(std::move(name), fmt,
   1734                             to->GetInterface()->GetNameString());
   1735  } else if (!name) {
   1736    XPCNativeSet* set = GetSet();
   1737    XPCNativeInterface** array = set->GetInterfaceArray();
   1738    uint16_t count = set->GetInterfaceCount();
   1739    MOZ_RELEASE_ASSERT(count >= 1, "Expected at least one interface");
   1740    MOZ_ASSERT(*array[0]->GetIID() == NS_GET_IID(nsISupports),
   1741               "The first interface must be nsISupports");
   1742 
   1743    // The first interface is always nsISupports, so don't print it, unless
   1744    // there are no others.
   1745    if (count == 1) {
   1746      name = JS_sprintf_append(std::move(name), "nsISupports");
   1747    } else if (count == 2) {
   1748      name =
   1749          JS_sprintf_append(std::move(name), "%s", array[1]->GetNameString());
   1750    } else {
   1751      for (uint16_t i = 1; i < count; i++) {
   1752        const char* fmt = (i == 1)           ? "(%s"
   1753                          : (i == count - 1) ? ", %s)"
   1754                                             : ", %s";
   1755        name =
   1756            JS_sprintf_append(std::move(name), fmt, array[i]->GetNameString());
   1757      }
   1758    }
   1759  }
   1760 
   1761  if (!name) {
   1762    return nullptr;
   1763  }
   1764  const char* fmt = "[xpconnect wrapped %s" FMT_ADDR FMT_STR(" (native")
   1765      FMT_ADDR FMT_STR(")") "]";
   1766  if (scr) {
   1767    fmt = "[object %s" FMT_ADDR FMT_STR(" (native") FMT_ADDR FMT_STR(")") "]";
   1768  }
   1769  sz =
   1770      JS_smprintf(fmt, name.get() PARAM_ADDR(this) PARAM_ADDR(mIdentity.get()));
   1771 
   1772  return sz.release();
   1773 
   1774 #undef FMT_ADDR
   1775 #undef PARAM_ADDR
   1776 }
   1777 
   1778 /***************************************************************************/
   1779 
   1780 #ifdef XPC_CHECK_CLASSINFO_CLAIMS
   1781 static void DEBUG_CheckClassInfoClaims(XPCWrappedNative* wrapper) {
   1782  if (!wrapper || !wrapper->GetClassInfo()) {
   1783    return;
   1784  }
   1785 
   1786  nsISupports* obj = wrapper->GetIdentityObject();
   1787  XPCNativeSet* set = wrapper->GetSet();
   1788  uint16_t count = set->GetInterfaceCount();
   1789  for (uint16_t i = 0; i < count; i++) {
   1790    nsIClassInfo* clsInfo = wrapper->GetClassInfo();
   1791    XPCNativeInterface* iface = set->GetInterfaceAt(i);
   1792    const nsXPTInterfaceInfo* info = iface->GetInterfaceInfo();
   1793    nsISupports* ptr;
   1794 
   1795    nsresult rv = obj->QueryInterface(info->IID(), (void**)&ptr);
   1796    if (NS_SUCCEEDED(rv)) {
   1797      NS_RELEASE(ptr);
   1798      continue;
   1799    }
   1800    if (rv == NS_ERROR_OUT_OF_MEMORY) {
   1801      continue;
   1802    }
   1803 
   1804    // Houston, We have a problem...
   1805 
   1806    char* className = nullptr;
   1807    char* contractID = nullptr;
   1808    const char* interfaceName = info->Name();
   1809 
   1810    clsInfo->GetContractID(&contractID);
   1811    if (wrapper->GetScriptable()) {
   1812      wrapper->GetScriptable()->GetClassName(&className);
   1813    }
   1814 
   1815    printf(
   1816        "\n!!! Object's nsIClassInfo lies about its interfaces!!!\n"
   1817        "   classname: %s \n"
   1818        "   contractid: %s \n"
   1819        "   unimplemented interface name: %s\n\n",
   1820        className ? className : "<unknown>",
   1821        contractID ? contractID : "<unknown>", interfaceName);
   1822 
   1823    if (className) {
   1824      free(className);
   1825    }
   1826    if (contractID) {
   1827      free(contractID);
   1828    }
   1829  }
   1830 }
   1831 #endif