WebIDLGlobalNameHash.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "jswrapper.h"
  6. #include "WebIDLGlobalNameHash.h"
  7. #include "js/GCAPI.h"
  8. #include "XrayWrapper.h"
  9. #include "XPCWrapper.h"
  10. #include "mozilla/dom/Selection.h"
  11. #include "mozilla/HashFunctions.h"
  12. #include "mozilla/Maybe.h"
  13. #include "mozilla/dom/DOMJSProxyHandler.h"
  14. #include "mozilla/dom/RegisterBindings.h"
  15. #include "nsIMemoryReporter.h"
  16. #include "nsTHashtable.h"
  17. namespace mozilla {
  18. namespace dom {
  19. struct MOZ_STACK_CLASS WebIDLNameTableKey
  20. {
  21. explicit WebIDLNameTableKey(JSFlatString* aJSString)
  22. : mLength(js::GetFlatStringLength(aJSString))
  23. {
  24. mNogc.emplace();
  25. JSLinearString* jsString = js::FlatStringToLinearString(aJSString);
  26. if (js::LinearStringHasLatin1Chars(jsString)) {
  27. mLatin1String = reinterpret_cast<const char*>(
  28. js::GetLatin1LinearStringChars(*mNogc, jsString));
  29. mTwoBytesString = nullptr;
  30. mHash = mLatin1String ? HashString(mLatin1String, mLength) : 0;
  31. } else {
  32. mLatin1String = nullptr;
  33. mTwoBytesString = js::GetTwoByteLinearStringChars(*mNogc, jsString);
  34. mHash = mTwoBytesString ? HashString(mTwoBytesString, mLength) : 0;
  35. }
  36. }
  37. explicit WebIDLNameTableKey(const char* aString, size_t aLength)
  38. : mLatin1String(aString),
  39. mTwoBytesString(nullptr),
  40. mLength(aLength),
  41. mHash(HashString(aString, aLength))
  42. {
  43. MOZ_ASSERT(aString[aLength] == '\0');
  44. }
  45. Maybe<JS::AutoCheckCannotGC> mNogc;
  46. const char* mLatin1String;
  47. const char16_t* mTwoBytesString;
  48. size_t mLength;
  49. uint32_t mHash;
  50. };
  51. struct WebIDLNameTableEntry : public PLDHashEntryHdr
  52. {
  53. typedef const WebIDLNameTableKey& KeyType;
  54. typedef const WebIDLNameTableKey* KeyTypePointer;
  55. explicit WebIDLNameTableEntry(KeyTypePointer aKey)
  56. : mNameOffset(0),
  57. mNameLength(0),
  58. mDefine(nullptr),
  59. mEnabled(nullptr)
  60. {}
  61. WebIDLNameTableEntry(WebIDLNameTableEntry&& aEntry)
  62. : mNameOffset(aEntry.mNameOffset),
  63. mNameLength(aEntry.mNameLength),
  64. mDefine(aEntry.mDefine),
  65. mEnabled(aEntry.mEnabled)
  66. {}
  67. ~WebIDLNameTableEntry()
  68. {}
  69. bool KeyEquals(KeyTypePointer aKey) const
  70. {
  71. if (mNameLength != aKey->mLength) {
  72. return false;
  73. }
  74. const char* name = WebIDLGlobalNameHash::sNames + mNameOffset;
  75. if (aKey->mLatin1String) {
  76. return PodEqual(aKey->mLatin1String, name, aKey->mLength);
  77. }
  78. return nsCharTraits<char16_t>::compareASCII(aKey->mTwoBytesString, name,
  79. aKey->mLength) == 0;
  80. }
  81. static KeyTypePointer KeyToPointer(KeyType aKey)
  82. {
  83. return &aKey;
  84. }
  85. static PLDHashNumber HashKey(KeyTypePointer aKey)
  86. {
  87. return aKey->mHash;
  88. }
  89. enum { ALLOW_MEMMOVE = true };
  90. uint16_t mNameOffset;
  91. uint16_t mNameLength;
  92. WebIDLGlobalNameHash::DefineGlobalName mDefine;
  93. // May be null if enabled unconditionally
  94. WebIDLGlobalNameHash::ConstructorEnabled* mEnabled;
  95. };
  96. static nsTHashtable<WebIDLNameTableEntry>* sWebIDLGlobalNames;
  97. class WebIDLGlobalNamesHashReporter final : public nsIMemoryReporter
  98. {
  99. MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf)
  100. ~WebIDLGlobalNamesHashReporter() {}
  101. public:
  102. NS_DECL_ISUPPORTS
  103. NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
  104. nsISupports* aData, bool aAnonymize) override
  105. {
  106. int64_t amount =
  107. sWebIDLGlobalNames ?
  108. sWebIDLGlobalNames->ShallowSizeOfIncludingThis(MallocSizeOf) : 0;
  109. MOZ_COLLECT_REPORT(
  110. "explicit/dom/webidl-globalnames", KIND_HEAP, UNITS_BYTES, amount,
  111. "Memory used by the hash table for WebIDL's global names.");
  112. return NS_OK;
  113. }
  114. };
  115. NS_IMPL_ISUPPORTS(WebIDLGlobalNamesHashReporter, nsIMemoryReporter)
  116. /* static */
  117. void
  118. WebIDLGlobalNameHash::Init()
  119. {
  120. sWebIDLGlobalNames = new nsTHashtable<WebIDLNameTableEntry>(sCount);
  121. RegisterWebIDLGlobalNames();
  122. RegisterStrongMemoryReporter(new WebIDLGlobalNamesHashReporter());
  123. }
  124. /* static */
  125. void
  126. WebIDLGlobalNameHash::Shutdown()
  127. {
  128. delete sWebIDLGlobalNames;
  129. }
  130. /* static */
  131. void
  132. WebIDLGlobalNameHash::Register(uint16_t aNameOffset, uint16_t aNameLength,
  133. DefineGlobalName aDefine,
  134. ConstructorEnabled* aEnabled)
  135. {
  136. const char* name = sNames + aNameOffset;
  137. WebIDLNameTableKey key(name, aNameLength);
  138. WebIDLNameTableEntry* entry = sWebIDLGlobalNames->PutEntry(key);
  139. entry->mNameOffset = aNameOffset;
  140. entry->mNameLength = aNameLength;
  141. entry->mDefine = aDefine;
  142. entry->mEnabled = aEnabled;
  143. }
  144. /* static */
  145. void
  146. WebIDLGlobalNameHash::Remove(const char* aName, uint32_t aLength)
  147. {
  148. WebIDLNameTableKey key(aName, aLength);
  149. sWebIDLGlobalNames->RemoveEntry(key);
  150. }
  151. /* static */
  152. bool
  153. WebIDLGlobalNameHash::DefineIfEnabled(JSContext* aCx,
  154. JS::Handle<JSObject*> aObj,
  155. JS::Handle<jsid> aId,
  156. JS::MutableHandle<JS::PropertyDescriptor> aDesc,
  157. bool* aFound)
  158. {
  159. MOZ_ASSERT(JSID_IS_STRING(aId), "Check for string id before calling this!");
  160. const WebIDLNameTableEntry* entry;
  161. {
  162. WebIDLNameTableKey key(JSID_TO_FLAT_STRING(aId));
  163. // Rooting analysis thinks nsTHashtable<...>::GetEntry may GC because it
  164. // ends up calling through PLDHashTableOps' matchEntry function pointer, but
  165. // we know WebIDLNameTableEntry::KeyEquals can't cause a GC.
  166. JS::AutoSuppressGCAnalysis suppress;
  167. entry = sWebIDLGlobalNames->GetEntry(key);
  168. }
  169. if (!entry) {
  170. *aFound = false;
  171. return true;
  172. }
  173. *aFound = true;
  174. ConstructorEnabled* checkEnabledForScope = entry->mEnabled;
  175. // We do the enabled check on the current compartment of aCx, but for the
  176. // actual object we pass in the underlying object in the Xray case. That
  177. // way the callee can decide whether to allow access based on the caller
  178. // or the window being touched.
  179. JS::Rooted<JSObject*> global(aCx,
  180. js::CheckedUnwrap(aObj, /* stopAtWindowProxy = */ false));
  181. if (!global) {
  182. return Throw(aCx, NS_ERROR_DOM_SECURITY_ERR);
  183. }
  184. {
  185. // It's safe to pass "&global" here, because we've already unwrapped it, but
  186. // for general sanity better to not have debug code even having the
  187. // appearance of mutating things that opt code uses.
  188. #ifdef DEBUG
  189. JS::Rooted<JSObject*> temp(aCx, global);
  190. DebugOnly<nsGlobalWindow*> win;
  191. MOZ_ASSERT(NS_SUCCEEDED(UNWRAP_OBJECT(Window, &temp, win)));
  192. #endif
  193. }
  194. if (checkEnabledForScope && !checkEnabledForScope(aCx, global)) {
  195. return true;
  196. }
  197. // The DOM constructor resolve machinery interacts with Xrays in tricky
  198. // ways, and there are some asymmetries that are important to understand.
  199. //
  200. // In the regular (non-Xray) case, we only want to resolve constructors
  201. // once (so that if they're deleted, they don't reappear). We do this by
  202. // stashing the constructor in a slot on the global, such that we can see
  203. // during resolve whether we've created it already. This is rather
  204. // memory-intensive, so we don't try to maintain these semantics when
  205. // manipulating a global over Xray (so the properties just re-resolve if
  206. // they've been deleted).
  207. //
  208. // Unfortunately, there's a bit of an impedance-mismatch between the Xray
  209. // and non-Xray machinery. The Xray machinery wants an API that returns a
  210. // JS::PropertyDescriptor, so that the resolve hook doesn't have to get
  211. // snared up with trying to define a property on the Xray holder. At the
  212. // same time, the DefineInterface callbacks are set up to define things
  213. // directly on the global. And re-jiggering them to return property
  214. // descriptors is tricky, because some DefineInterface callbacks define
  215. // multiple things (like the Image() alias for HTMLImageElement).
  216. //
  217. // So the setup is as-follows:
  218. //
  219. // * The resolve function takes a JS::PropertyDescriptor, but in the
  220. // non-Xray case, callees may define things directly on the global, and
  221. // set the value on the property descriptor to |undefined| to indicate
  222. // that there's nothing more for the caller to do. We assert against
  223. // this behavior in the Xray case.
  224. //
  225. // * We make sure that we do a non-Xray resolve first, so that all the
  226. // slots are set up. In the Xray case, this means unwrapping and doing
  227. // a non-Xray resolve before doing the Xray resolve.
  228. //
  229. // This all could use some grand refactoring, but for now we just limp
  230. // along.
  231. if (xpc::WrapperFactory::IsXrayWrapper(aObj)) {
  232. JS::Rooted<JSObject*> interfaceObject(aCx);
  233. {
  234. JSAutoCompartment ac(aCx, global);
  235. interfaceObject = entry->mDefine(aCx, global, aId, false);
  236. }
  237. if (NS_WARN_IF(!interfaceObject)) {
  238. return Throw(aCx, NS_ERROR_FAILURE);
  239. }
  240. if (!JS_WrapObject(aCx, &interfaceObject)) {
  241. return Throw(aCx, NS_ERROR_FAILURE);
  242. }
  243. FillPropertyDescriptor(aDesc, aObj, 0, JS::ObjectValue(*interfaceObject));
  244. return true;
  245. }
  246. JS::Rooted<JSObject*> interfaceObject(aCx,
  247. entry->mDefine(aCx, aObj, aId, true));
  248. if (NS_WARN_IF(!interfaceObject)) {
  249. return Throw(aCx, NS_ERROR_FAILURE);
  250. }
  251. // We've already defined the property. We indicate this to the caller
  252. // by filling a property descriptor with JS::UndefinedValue() as the
  253. // value. We still have to fill in a property descriptor, though, so
  254. // that the caller knows the property is in fact on this object. It
  255. // doesn't matter what we pass for the "readonly" argument here.
  256. FillPropertyDescriptor(aDesc, aObj, JS::UndefinedValue(), false);
  257. return true;
  258. }
  259. /* static */
  260. bool
  261. WebIDLGlobalNameHash::MayResolve(jsid aId)
  262. {
  263. WebIDLNameTableKey key(JSID_TO_FLAT_STRING(aId));
  264. // Rooting analysis thinks nsTHashtable<...>::Contains may GC because it ends
  265. // up calling through PLDHashTableOps' matchEntry function pointer, but we
  266. // know WebIDLNameTableEntry::KeyEquals can't cause a GC.
  267. JS::AutoSuppressGCAnalysis suppress;
  268. return sWebIDLGlobalNames->Contains(key);
  269. }
  270. /* static */
  271. void
  272. WebIDLGlobalNameHash::GetNames(JSContext* aCx, JS::Handle<JSObject*> aObj,
  273. nsTArray<nsString>& aNames)
  274. {
  275. for (auto iter = sWebIDLGlobalNames->Iter(); !iter.Done(); iter.Next()) {
  276. const WebIDLNameTableEntry* entry = iter.Get();
  277. if (!entry->mEnabled || entry->mEnabled(aCx, aObj)) {
  278. AppendASCIItoUTF16(nsDependentCString(sNames + entry->mNameOffset,
  279. entry->mNameLength),
  280. *aNames.AppendElement());
  281. }
  282. }
  283. }
  284. } // namespace dom
  285. } // namespace mozilla