summaryrefslogtreecommitdiffstats
path: root/dom/bindings/WebIDLGlobalNameHash.cpp
blob: c9b1b7d438a29480b5473a9f01595fda9331fc60 (plain)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */

#include "jswrapper.h"
#include "WebIDLGlobalNameHash.h"
#include "js/GCAPI.h"
#include "XrayWrapper.h"
#include "XPCWrapper.h"
#include "mozilla/dom/Selection.h"
#include "mozilla/HashFunctions.h"
#include "mozilla/Maybe.h"
#include "mozilla/dom/DOMJSProxyHandler.h"
#include "mozilla/dom/RegisterBindings.h"
#include "nsIMemoryReporter.h"
#include "nsTHashtable.h"

namespace mozilla {
namespace dom {

struct MOZ_STACK_CLASS WebIDLNameTableKey
{
  explicit WebIDLNameTableKey(JSFlatString* aJSString)
    : mLength(js::GetFlatStringLength(aJSString))
  {
    mNogc.emplace();
    JSLinearString* jsString = js::FlatStringToLinearString(aJSString);
    if (js::LinearStringHasLatin1Chars(jsString)) {
      mLatin1String = reinterpret_cast<const char*>(
        js::GetLatin1LinearStringChars(*mNogc, jsString));
      mTwoBytesString = nullptr;
      mHash = mLatin1String ? HashString(mLatin1String, mLength) : 0;
    } else {
      mLatin1String = nullptr;
      mTwoBytesString = js::GetTwoByteLinearStringChars(*mNogc, jsString);
      mHash = mTwoBytesString ? HashString(mTwoBytesString, mLength) : 0;
    }
  }
  explicit WebIDLNameTableKey(const char* aString, size_t aLength)
    : mLatin1String(aString),
      mTwoBytesString(nullptr),
      mLength(aLength),
      mHash(HashString(aString, aLength))
  {
    MOZ_ASSERT(aString[aLength] == '\0');
  }

  Maybe<JS::AutoCheckCannotGC> mNogc;
  const char* mLatin1String;
  const char16_t* mTwoBytesString;
  size_t mLength;
  uint32_t mHash;
};

struct WebIDLNameTableEntry : public PLDHashEntryHdr
{
  typedef const WebIDLNameTableKey& KeyType;
  typedef const WebIDLNameTableKey* KeyTypePointer;

  explicit WebIDLNameTableEntry(KeyTypePointer aKey)
    : mNameOffset(0),
      mNameLength(0),
      mDefine(nullptr),
      mEnabled(nullptr)
  {}
  WebIDLNameTableEntry(WebIDLNameTableEntry&& aEntry)
    : mNameOffset(aEntry.mNameOffset),
      mNameLength(aEntry.mNameLength),
      mDefine(aEntry.mDefine),
      mEnabled(aEntry.mEnabled)
  {}
  ~WebIDLNameTableEntry()
  {}

  bool KeyEquals(KeyTypePointer aKey) const
  {
    if (mNameLength != aKey->mLength) {
      return false;
    }

    const char* name = WebIDLGlobalNameHash::sNames + mNameOffset;

    if (aKey->mLatin1String) {
      return PodEqual(aKey->mLatin1String, name, aKey->mLength);
    }

    return nsCharTraits<char16_t>::compareASCII(aKey->mTwoBytesString, name,
                                                aKey->mLength) == 0;
  }

  static KeyTypePointer KeyToPointer(KeyType aKey)
  {
    return &aKey;
  }

  static PLDHashNumber HashKey(KeyTypePointer aKey)
  {
    return aKey->mHash;
  }

  enum { ALLOW_MEMMOVE = true };

  uint16_t mNameOffset;
  uint16_t mNameLength;
  WebIDLGlobalNameHash::DefineGlobalName mDefine;
  // May be null if enabled unconditionally
  WebIDLGlobalNameHash::ConstructorEnabled* mEnabled;
};

static nsTHashtable<WebIDLNameTableEntry>* sWebIDLGlobalNames;

class WebIDLGlobalNamesHashReporter final : public nsIMemoryReporter
{
  MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf)

  ~WebIDLGlobalNamesHashReporter() {}

public:
  NS_DECL_ISUPPORTS

  NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
                            nsISupports* aData, bool aAnonymize) override
  {
    int64_t amount =
      sWebIDLGlobalNames ?
      sWebIDLGlobalNames->ShallowSizeOfIncludingThis(MallocSizeOf) : 0;

    MOZ_COLLECT_REPORT(
      "explicit/dom/webidl-globalnames", KIND_HEAP, UNITS_BYTES, amount,
      "Memory used by the hash table for WebIDL's global names.");

    return NS_OK;
  }
};

NS_IMPL_ISUPPORTS(WebIDLGlobalNamesHashReporter, nsIMemoryReporter)

/* static */
void
WebIDLGlobalNameHash::Init()
{
  sWebIDLGlobalNames = new nsTHashtable<WebIDLNameTableEntry>(sCount);
  RegisterWebIDLGlobalNames();

  RegisterStrongMemoryReporter(new WebIDLGlobalNamesHashReporter());
}

/* static */
void
WebIDLGlobalNameHash::Shutdown()
{
  delete sWebIDLGlobalNames;
}

/* static */
void
WebIDLGlobalNameHash::Register(uint16_t aNameOffset, uint16_t aNameLength,
                               DefineGlobalName aDefine,
                               ConstructorEnabled* aEnabled)
{
  const char* name = sNames + aNameOffset;
  WebIDLNameTableKey key(name, aNameLength);
  WebIDLNameTableEntry* entry = sWebIDLGlobalNames->PutEntry(key);
  entry->mNameOffset = aNameOffset;
  entry->mNameLength = aNameLength;
  entry->mDefine = aDefine;
  entry->mEnabled = aEnabled;
}

/* static */
void
WebIDLGlobalNameHash::Remove(const char* aName, uint32_t aLength)
{
  WebIDLNameTableKey key(aName, aLength);
  sWebIDLGlobalNames->RemoveEntry(key);
}

/* static */
bool
WebIDLGlobalNameHash::DefineIfEnabled(JSContext* aCx,
                                      JS::Handle<JSObject*> aObj,
                                      JS::Handle<jsid> aId,
                                      JS::MutableHandle<JS::PropertyDescriptor> aDesc,
                                      bool* aFound)
{
  MOZ_ASSERT(JSID_IS_STRING(aId), "Check for string id before calling this!");

  const WebIDLNameTableEntry* entry;
  {
    WebIDLNameTableKey key(JSID_TO_FLAT_STRING(aId));
    // Rooting analysis thinks nsTHashtable<...>::GetEntry may GC because it
    // ends up calling through PLDHashTableOps' matchEntry function pointer, but
    // we know WebIDLNameTableEntry::KeyEquals can't cause a GC.
    JS::AutoSuppressGCAnalysis suppress;
    entry = sWebIDLGlobalNames->GetEntry(key);
  }

  if (!entry) {
    *aFound = false;
    return true;
  }

  *aFound = true;

  ConstructorEnabled* checkEnabledForScope = entry->mEnabled;
  // We do the enabled check on the current compartment of aCx, but for the
  // actual object we pass in the underlying object in the Xray case.  That
  // way the callee can decide whether to allow access based on the caller
  // or the window being touched.
  JS::Rooted<JSObject*> global(aCx,
    js::CheckedUnwrap(aObj, /* stopAtWindowProxy = */ false));
  if (!global) {
    return Throw(aCx, NS_ERROR_DOM_SECURITY_ERR);
  }

  {
    // It's safe to pass "&global" here, because we've already unwrapped it, but
    // for general sanity better to not have debug code even having the
    // appearance of mutating things that opt code uses.
#ifdef DEBUG
    JS::Rooted<JSObject*> temp(aCx, global);
    DebugOnly<nsGlobalWindow*> win;
    MOZ_ASSERT(NS_SUCCEEDED(UNWRAP_OBJECT(Window, &temp, win)));
#endif
  }

  if (checkEnabledForScope && !checkEnabledForScope(aCx, global)) {
    return true;
  }

  // The DOM constructor resolve machinery interacts with Xrays in tricky
  // ways, and there are some asymmetries that are important to understand.
  //
  // In the regular (non-Xray) case, we only want to resolve constructors
  // once (so that if they're deleted, they don't reappear). We do this by
  // stashing the constructor in a slot on the global, such that we can see
  // during resolve whether we've created it already. This is rather
  // memory-intensive, so we don't try to maintain these semantics when
  // manipulating a global over Xray (so the properties just re-resolve if
  // they've been deleted).
  //
  // Unfortunately, there's a bit of an impedance-mismatch between the Xray
  // and non-Xray machinery. The Xray machinery wants an API that returns a
  // JS::PropertyDescriptor, so that the resolve hook doesn't have to get
  // snared up with trying to define a property on the Xray holder. At the
  // same time, the DefineInterface callbacks are set up to define things
  // directly on the global.  And re-jiggering them to return property
  // descriptors is tricky, because some DefineInterface callbacks define
  // multiple things (like the Image() alias for HTMLImageElement).
  //
  // So the setup is as-follows:
  //
  // * The resolve function takes a JS::PropertyDescriptor, but in the
  //   non-Xray case, callees may define things directly on the global, and
  //   set the value on the property descriptor to |undefined| to indicate
  //   that there's nothing more for the caller to do. We assert against
  //   this behavior in the Xray case.
  //
  // * We make sure that we do a non-Xray resolve first, so that all the
  //   slots are set up. In the Xray case, this means unwrapping and doing
  //   a non-Xray resolve before doing the Xray resolve.
  //
  // This all could use some grand refactoring, but for now we just limp
  // along.
  if (xpc::WrapperFactory::IsXrayWrapper(aObj)) {
    JS::Rooted<JSObject*> interfaceObject(aCx);
    {
      JSAutoCompartment ac(aCx, global);
      interfaceObject = entry->mDefine(aCx, global, aId, false);
    }
    if (NS_WARN_IF(!interfaceObject)) {
      return Throw(aCx, NS_ERROR_FAILURE);
    }
    if (!JS_WrapObject(aCx, &interfaceObject)) {
      return Throw(aCx, NS_ERROR_FAILURE);
    }

    FillPropertyDescriptor(aDesc, aObj, 0, JS::ObjectValue(*interfaceObject));
    return true;
  }

  JS::Rooted<JSObject*> interfaceObject(aCx,
                                        entry->mDefine(aCx, aObj, aId, true));
  if (NS_WARN_IF(!interfaceObject)) {
    return Throw(aCx, NS_ERROR_FAILURE);
  }

  // We've already defined the property.  We indicate this to the caller
  // by filling a property descriptor with JS::UndefinedValue() as the
  // value.  We still have to fill in a property descriptor, though, so
  // that the caller knows the property is in fact on this object.  It
  // doesn't matter what we pass for the "readonly" argument here.
  FillPropertyDescriptor(aDesc, aObj, JS::UndefinedValue(), false);

  return true;
}

/* static */
bool
WebIDLGlobalNameHash::MayResolve(jsid aId)
{
  WebIDLNameTableKey key(JSID_TO_FLAT_STRING(aId));
  // Rooting analysis thinks nsTHashtable<...>::Contains may GC because it ends
  // up calling through PLDHashTableOps' matchEntry function pointer, but we
  // know WebIDLNameTableEntry::KeyEquals can't cause a GC.
  JS::AutoSuppressGCAnalysis suppress;
  return sWebIDLGlobalNames->Contains(key);
}

/* static */
void
WebIDLGlobalNameHash::GetNames(JSContext* aCx, JS::Handle<JSObject*> aObj,
                               nsTArray<nsString>& aNames)
{
  for (auto iter = sWebIDLGlobalNames->Iter(); !iter.Done(); iter.Next()) {
    const WebIDLNameTableEntry* entry = iter.Get();
    if (!entry->mEnabled || entry->mEnabled(aCx, aObj)) {
      AppendASCIItoUTF16(nsDependentCString(sNames + entry->mNameOffset,
                                            entry->mNameLength),
                         *aNames.AppendElement());
    }
  }
}

} // namespace dom
} // namespace mozilla