| 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 |
#ifndef vm_Compartment_inl_h |
| 8 |
#define vm_Compartment_inl_h |
| 9 |
|
| 10 |
#include "vm/Compartment.h" |
| 11 |
|
| 12 |
#include <type_traits> |
| 13 |
|
| 14 |
#include "jsapi.h" |
| 15 |
#include "jsfriendapi.h" |
| 16 |
#include "jsnum.h" |
| 17 |
#include "js/CallArgs.h" |
| 18 |
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_* |
| 19 |
#include "js/Wrapper.h" |
| 20 |
#include "vm/Iteration.h" |
| 21 |
#include "vm/JSObject.h" |
| 22 |
|
| 23 |
#include "vm/JSContext-inl.h" |
| 24 |
|
| 25 |
struct JSClass; |
| 26 |
|
| 27 |
inline js::StringWrapperMap::Ptr JS::Compartment::lookupWrapper( |
| 28 |
JSString* str) const { |
| 29 |
return zone()->crossZoneStringWrappers().lookup(str); |
| 30 |
} |
| 31 |
|
| 32 |
inline bool JS::Compartment::wrap(JSContext* cx, JS::MutableHandleValue vp) { |
| 33 |
/* Only GC things have to be wrapped or copied. */ |
| 34 |
if (!vp.isGCThing()) { |
| 35 |
return true; |
| 36 |
} |
| 37 |
|
| 38 |
/* |
| 39 |
* Symbols are GC things, but never need to be wrapped or copied because |
| 40 |
* they are always allocated in the atoms zone. They still need to be |
| 41 |
* marked in the new compartment's zone, however. |
| 42 |
*/ |
| 43 |
if (vp.isSymbol()) { |
| 44 |
cx->markAtomValue(vp); |
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
/* Handle strings. */ |
| 49 |
if (vp.isString()) { |
| 50 |
JS::RootedString str(cx, vp.toString()); |
| 51 |
if (!wrap(cx, &str)) { |
| 52 |
return false; |
| 53 |
} |
| 54 |
vp.setString(str); |
| 55 |
return true; |
| 56 |
} |
| 57 |
|
| 58 |
if (vp.isBigInt()) { |
| 59 |
JS::RootedBigInt bi(cx, vp.toBigInt()); |
| 60 |
if (!wrap(cx, &bi)) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
vp.setBigInt(bi); |
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
MOZ_ASSERT(vp.isObject()); |
| 68 |
|
| 69 |
/* |
| 70 |
* All that's left are objects. |
| 71 |
* |
| 72 |
* Object wrapping isn't the fastest thing in the world, in part because |
| 73 |
* we have to unwrap and invoke the prewrap hook to find the identity |
| 74 |
* object before we even start checking the cache. Neither of these |
| 75 |
* operations are needed in the common case, where we're just wrapping |
| 76 |
* a plain JS object from the wrappee's side of the membrane to the |
| 77 |
* wrapper's side. |
| 78 |
* |
| 79 |
* To optimize this, we note that the cache should only ever contain |
| 80 |
* identity objects - that is to say, objects that serve as the |
| 81 |
* canonical representation for a unique object identity observable by |
| 82 |
* script. Unwrap and prewrap are both steps that we take to get to the |
| 83 |
* identity of an incoming objects, and as such, they shuld never map |
| 84 |
* one identity object to another object. This means that we can safely |
| 85 |
* check the cache immediately, and only risk false negatives. Do this |
| 86 |
* in opt builds, and do both in debug builds so that we can assert |
| 87 |
* that we get the same answer. |
| 88 |
*/ |
| 89 |
#ifdef DEBUG |
| 90 |
JS::AssertValueIsNotGray(vp); |
| 91 |
JS::RootedObject cacheResult(cx); |
| 92 |
#endif |
| 93 |
if (js::ObjectWrapperMap::Ptr p = lookupWrapper(&vp.toObject())) { |
| 94 |
#ifdef DEBUG |
| 95 |
cacheResult = p->value().get(); |
| 96 |
#else |
| 97 |
vp.setObject(*p->value().get()); |
| 98 |
return true; |
| 99 |
#endif |
| 100 |
} |
| 101 |
|
| 102 |
JS::RootedObject obj(cx, &vp.toObject()); |
| 103 |
if (!wrap(cx, &obj)) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
vp.setObject(*obj); |
| 107 |
MOZ_ASSERT_IF(cacheResult, obj == cacheResult); |
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
inline bool JS::Compartment::wrap(JSContext* cx, |
| 112 |
MutableHandle<mozilla::Maybe<Value>> vp) { |
| 113 |
if (vp.get().isNothing()) { |
| 114 |
return true; |
| 115 |
} |
| 116 |
|
| 117 |
return wrap(cx, MutableHandle<Value>::fromMarkedLocation(vp.get().ptr())); |
| 118 |
} |
| 119 |
|
| 120 |
namespace js { |
| 121 |
namespace detail { |
| 122 |
|
| 123 |
/** |
| 124 |
* Return the name of class T as a static null-terminated ASCII string constant |
| 125 |
* (for error messages). |
| 126 |
*/ |
| 127 |
template <class T> |
| 128 |
const char* ClassName() { |
| 129 |
return T::class_.name; |
| 130 |
} |
| 131 |
|
| 132 |
template <class T, class ErrorCallback> |
| 133 |
[[nodiscard]] T* UnwrapAndTypeCheckValueSlowPath(JSContext* cx, |
| 134 |
HandleValue value, |
| 135 |
ErrorCallback throwTypeError) { |
| 136 |
JSObject* obj = nullptr; |
| 137 |
if (value.isObject()) { |
| 138 |
obj = &value.toObject(); |
| 139 |
if (IsWrapper(obj)) { |
| 140 |
obj = CheckedUnwrapStatic(obj); |
| 141 |
if (!obj) { |
| 142 |
ReportAccessDenied(cx); |
| 143 |
return nullptr; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
if (!obj || !obj->is<T>()) { |
| 149 |
throwTypeError(); |
| 150 |
return nullptr; |
| 151 |
} |
| 152 |
|
| 153 |
return &obj->as<T>(); |
| 154 |
} |
| 155 |
|
| 156 |
template <class ErrorCallback> |
| 157 |
[[nodiscard]] JSObject* UnwrapAndTypeCheckValueSlowPath( |
| 158 |
JSContext* cx, HandleValue value, const JSClass* clasp, |
| 159 |
ErrorCallback throwTypeError) { |
| 160 |
JSObject* obj = nullptr; |
| 161 |
if (value.isObject()) { |
| 162 |
obj = &value.toObject(); |
| 163 |
if (IsWrapper(obj)) { |
| 164 |
obj = CheckedUnwrapStatic(obj); |
| 165 |
if (!obj) { |
| 166 |
ReportAccessDenied(cx); |
| 167 |
return nullptr; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
if (!obj || !obj->hasClass(clasp)) { |
| 173 |
throwTypeError(); |
| 174 |
return nullptr; |
| 175 |
} |
| 176 |
|
| 177 |
return obj; |
| 178 |
} |
| 179 |
|
| 180 |
} // namespace detail |
| 181 |
|
| 182 |
/** |
| 183 |
* Remove all wrappers from `val` and try to downcast the result to class `T`. |
| 184 |
* |
| 185 |
* DANGER: The result may not be same-compartment with `cx`. |
| 186 |
* |
| 187 |
* This calls `throwTypeError` if the value isn't an object, cannot be |
| 188 |
* unwrapped, or isn't an instance of the expected type. `throwTypeError` must |
| 189 |
* in fact throw a TypeError (or OOM trying). |
| 190 |
*/ |
| 191 |
template <class T, class ErrorCallback> |
| 192 |
[[nodiscard]] inline T* UnwrapAndTypeCheckValue(JSContext* cx, |
| 193 |
HandleValue value, |
| 194 |
ErrorCallback throwTypeError) { |
| 195 |
cx->check(value); |
| 196 |
|
| 197 |
static_assert(!std::is_convertible_v<T*, Wrapper*>, |
| 198 |
"T can't be a Wrapper type; this function discards wrappers"); |
| 199 |
|
| 200 |
if (value.isObject() && value.toObject().is<T>()) { |
| 201 |
return &value.toObject().as<T>(); |
| 202 |
} |
| 203 |
|
| 204 |
return detail::UnwrapAndTypeCheckValueSlowPath<T>(cx, value, throwTypeError); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Remove all wrappers from |val| and try to downcast the result to an object of |
| 209 |
* the class |clasp|. |
| 210 |
* |
| 211 |
* DANGER: The result may not be same-compartment with |cx|. |
| 212 |
* |
| 213 |
* This calls |throwTypeError| if the value isn't an object, cannot be |
| 214 |
* unwrapped, or isn't an instance of the expected type. |throwTypeError| must |
| 215 |
* in fact throw a TypeError (or OOM trying). |
| 216 |
*/ |
| 217 |
template <class ErrorCallback> |
| 218 |
[[nodiscard]] inline JSObject* UnwrapAndTypeCheckValue( |
| 219 |
JSContext* cx, HandleValue value, const JSClass* clasp, |
| 220 |
ErrorCallback throwTypeError) { |
| 221 |
cx->check(value); |
| 222 |
|
| 223 |
if (value.isObject() && value.toObject().hasClass(clasp)) { |
| 224 |
return &value.toObject(); |
| 225 |
} |
| 226 |
|
| 227 |
return detail::UnwrapAndTypeCheckValueSlowPath(cx, value, clasp, |
| 228 |
throwTypeError); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Remove all wrappers from `args.thisv()` and try to downcast the result to |
| 233 |
* class `T`. |
| 234 |
* |
| 235 |
* DANGER: The result may not be same-compartment with `cx`. |
| 236 |
* |
| 237 |
* This throws a TypeError if the value isn't an object, cannot be unwrapped, |
| 238 |
* or isn't an instance of the expected type. |
| 239 |
*/ |
| 240 |
template <class T> |
| 241 |
[[nodiscard]] inline T* UnwrapAndTypeCheckThis(JSContext* cx, |
| 242 |
const CallArgs& args, |
| 243 |
const char* methodName) { |
| 244 |
HandleValue thisv = args.thisv(); |
| 245 |
return UnwrapAndTypeCheckValue<T>(cx, thisv, [cx, methodName, thisv] { |
| 246 |
JS_ReportErrorNumberLatin1(cx, GetErrorMessage, nullptr, |
| 247 |
JSMSG_INCOMPATIBLE_PROTO, detail::ClassName<T>(), |
| 248 |
methodName, InformalValueTypeName(thisv)); |
| 249 |
}); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Remove all wrappers from `args[argIndex]` and try to downcast the result to |
| 254 |
* class `T`. |
| 255 |
* |
| 256 |
* DANGER: The result may not be same-compartment with `cx`. |
| 257 |
* |
| 258 |
* This throws a TypeError if the specified argument is missing, isn't an |
| 259 |
* object, cannot be unwrapped, or isn't an instance of the expected type. |
| 260 |
*/ |
| 261 |
template <class T> |
| 262 |
[[nodiscard]] inline T* UnwrapAndTypeCheckArgument(JSContext* cx, |
| 263 |
CallArgs& args, |
| 264 |
const char* methodName, |
| 265 |
int argIndex) { |
| 266 |
HandleValue val = args.get(argIndex); |
| 267 |
return UnwrapAndTypeCheckValue<T>(cx, val, [cx, val, methodName, argIndex] { |
| 268 |
Int32ToCStringBuf cbuf; |
| 269 |
char* numStr = Int32ToCString(&cbuf, argIndex + 1); |
| 270 |
MOZ_ASSERT(numStr); |
| 271 |
JS_ReportErrorNumberLatin1( |
| 272 |
cx, GetErrorMessage, nullptr, JSMSG_WRONG_TYPE_ARG, numStr, methodName, |
| 273 |
detail::ClassName<T>(), InformalValueTypeName(val)); |
| 274 |
}); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Unwrap an object of a known type. |
| 279 |
* |
| 280 |
* If `obj` is an object of class T, this returns a pointer to that object. If |
| 281 |
* `obj` is a wrapper for such an object, this tries to unwrap the object and |
| 282 |
* return a pointer to it. If access is denied, or `obj` was a wrapper but has |
| 283 |
* been nuked, this reports an error and returns null. |
| 284 |
* |
| 285 |
* In all other cases, the behavior is undefined, so call this only if `obj` is |
| 286 |
* known to have been an object of class T, or a wrapper to a T, at some point. |
| 287 |
*/ |
| 288 |
template <class T> |
| 289 |
[[nodiscard]] inline T* UnwrapAndDowncastObject(JSContext* cx, JSObject* obj) { |
| 290 |
static_assert(!std::is_convertible_v<T*, Wrapper*>, |
| 291 |
"T can't be a Wrapper type; this function discards wrappers"); |
| 292 |
|
| 293 |
if (IsProxy(obj)) { |
| 294 |
if (JS_IsDeadWrapper(obj)) { |
| 295 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 296 |
JSMSG_DEAD_OBJECT); |
| 297 |
return nullptr; |
| 298 |
} |
| 299 |
|
| 300 |
// It would probably be OK to do an unchecked unwrap here, but we allow |
| 301 |
// arbitrary security policies, so check anyway. |
| 302 |
obj = obj->maybeUnwrapAs<T>(); |
| 303 |
if (!obj) { |
| 304 |
ReportAccessDenied(cx); |
| 305 |
return nullptr; |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
return &obj->as<T>(); |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Unwrap an object of a known (but not compile-time-known) class. |
| 314 |
* |
| 315 |
* If |obj| is an object with class |clasp|, this returns |obj|. If |obj| is a |
| 316 |
* wrapper for such an object, this tries to unwrap the object and return a |
| 317 |
* pointer to it. If access is denied, or |obj| was a wrapper but has been |
| 318 |
* nuked, this reports an error and returns null. |
| 319 |
* |
| 320 |
* In all other cases, the behavior is undefined, so call this only if |obj| is |
| 321 |
* known to have had class |clasp|, or been a wrapper to such an object, at some |
| 322 |
* point. |
| 323 |
*/ |
| 324 |
[[nodiscard]] inline JSObject* UnwrapAndDowncastObject(JSContext* cx, |
| 325 |
JSObject* obj, |
| 326 |
const JSClass* clasp) { |
| 327 |
if (IsProxy(obj)) { |
| 328 |
if (JS_IsDeadWrapper(obj)) { |
| 329 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 330 |
JSMSG_DEAD_OBJECT); |
| 331 |
return nullptr; |
| 332 |
} |
| 333 |
|
| 334 |
// It would probably be OK to do an unchecked unwrap here, but we allow |
| 335 |
// arbitrary security policies, so check anyway. |
| 336 |
obj = obj->maybeUnwrapAs(clasp); |
| 337 |
if (!obj) { |
| 338 |
ReportAccessDenied(cx); |
| 339 |
return nullptr; |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
MOZ_ASSERT(obj->hasClass(clasp)); |
| 344 |
return obj; |
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* Unwrap a value of a known type. See UnwrapAndDowncastObject. |
| 349 |
*/ |
| 350 |
template <class T> |
| 351 |
[[nodiscard]] inline T* UnwrapAndDowncastValue(JSContext* cx, |
| 352 |
const Value& value) { |
| 353 |
return UnwrapAndDowncastObject<T>(cx, &value.toObject()); |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Unwrap an object of a known (but not compile-time-known) class. See |
| 358 |
* UnwrapAndDowncastObject. |
| 359 |
*/ |
| 360 |
[[nodiscard]] inline JSObject* UnwrapAndDowncastValue(JSContext* cx, |
| 361 |
const Value& value, |
| 362 |
const JSClass* clasp) { |
| 363 |
return UnwrapAndDowncastObject(cx, &value.toObject(), clasp); |
| 364 |
} |
| 365 |
|
| 366 |
} // namespace js |
| 367 |
|
| 368 |
MOZ_ALWAYS_INLINE bool JS::Compartment::objectMaybeInIteration(JSObject* obj) { |
| 369 |
MOZ_ASSERT(obj->compartment() == this); |
| 370 |
|
| 371 |
js::NativeIteratorListIter iter(&enumerators_); |
| 372 |
|
| 373 |
// If the list is empty, we're not iterating any objects. |
| 374 |
if (iter.done()) { |
| 375 |
return false; |
| 376 |
} |
| 377 |
|
| 378 |
// If the list contains a single object, check if it's |obj|. |
| 379 |
js::NativeIterator* next = iter.next(); |
| 380 |
if (iter.done()) { |
| 381 |
return next->objectBeingIterated() == obj; |
| 382 |
} |
| 383 |
|
| 384 |
return true; |
| 385 |
} |
| 386 |
|
| 387 |
#endif /* vm_Compartment_inl_h */ |