Proxy.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * vim: set ts=8 sts=4 et sw=4 tw=99:
  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. #ifndef js_Proxy_h
  7. #define js_Proxy_h
  8. #include "mozilla/Maybe.h"
  9. #include "jsfriendapi.h"
  10. #include "js/CallNonGenericMethod.h"
  11. #include "js/Class.h"
  12. namespace js {
  13. using JS::AutoIdVector;
  14. using JS::CallArgs;
  15. using JS::Handle;
  16. using JS::HandleId;
  17. using JS::HandleObject;
  18. using JS::HandleValue;
  19. using JS::IsAcceptableThis;
  20. using JS::MutableHandle;
  21. using JS::MutableHandleObject;
  22. using JS::MutableHandleValue;
  23. using JS::NativeImpl;
  24. using JS::ObjectOpResult;
  25. using JS::PrivateValue;
  26. using JS::PropertyDescriptor;
  27. using JS::Value;
  28. class RegExpGuard;
  29. class JS_FRIEND_API(Wrapper);
  30. /*
  31. * A proxy is a JSObject with highly customizable behavior. ES6 specifies a
  32. * single kind of proxy, but the customization mechanisms we use to implement
  33. * ES6 Proxy objects are also useful wherever an object with weird behavior is
  34. * wanted. Proxies are used to implement:
  35. *
  36. * - the scope objects used by the Debugger's frame.eval() method
  37. * (see js::GetDebugScopeForFunction)
  38. *
  39. * - the khuey hack, whereby a whole compartment can be blown away
  40. * even if other compartments hold references to objects in it
  41. * (see js::NukeCrossCompartmentWrappers)
  42. *
  43. * - XPConnect security wrappers, which protect chrome from malicious content
  44. * (js/xpconnect/wrappers)
  45. *
  46. * - DOM objects with special property behavior, like named getters
  47. * (dom/bindings/Codegen.py generates these proxies from WebIDL)
  48. *
  49. * - semi-transparent use of objects that live in other processes
  50. * (CPOWs, implemented in js/ipc)
  51. *
  52. * ### Proxies and internal methods
  53. *
  54. * ES2016 specifies 13 internal methods. The runtime semantics of just
  55. * about everything a script can do to an object is specified in terms
  56. * of these internal methods. For example:
  57. *
  58. * JS code ES6 internal method that gets called
  59. * --------------------------- --------------------------------
  60. * obj.prop obj.[[Get]](obj, "prop")
  61. * "prop" in obj obj.[[HasProperty]]("prop")
  62. * new obj() obj.[[Construct]](<empty argument List>)
  63. *
  64. * With regard to the implementation of these internal methods, there are three
  65. * very different kinds of object in SpiderMonkey.
  66. *
  67. * 1. Native objects' internal methods are implemented in vm/NativeObject.cpp,
  68. * with duplicate (but functionally identical) implementations scattered
  69. * through the ICs and JITs.
  70. *
  71. * 2. Certain non-native objects have internal methods that are implemented as
  72. * magical js::ObjectOps hooks. We're trying to get rid of these.
  73. *
  74. * 3. All other objects are proxies. A proxy's internal methods are
  75. * implemented in C++, as the virtual methods of a C++ object stored on the
  76. * proxy, known as its handler.
  77. *
  78. * This means that just about anything you do to a proxy will end up going
  79. * through a C++ virtual method call. Possibly several. There's no reason the
  80. * JITs and ICs can't specialize for particular proxies, based on the handler;
  81. * but currently we don't do much of this, so the virtual method overhead
  82. * typically is actually incurred.
  83. *
  84. * ### The proxy handler hierarchy
  85. *
  86. * A major use case for proxies is to forward each internal method call to
  87. * another object, known as its target. The target can be an arbitrary JS
  88. * object. Not every proxy has the notion of a target, however.
  89. *
  90. * To minimize code duplication, a set of abstract proxy handler classes is
  91. * provided, from which other handlers may inherit. These abstract classes are
  92. * organized in the following hierarchy:
  93. *
  94. * BaseProxyHandler
  95. * |
  96. * Wrapper // has a target, can be unwrapped to reveal
  97. * | // target (see js::CheckedUnwrap)
  98. * |
  99. * CrossCompartmentWrapper // target is in another compartment;
  100. * // implements membrane between compartments
  101. *
  102. * Example: Some DOM objects (including all the arraylike DOM objects) are
  103. * implemented as proxies. Since these objects don't need to forward operations
  104. * to any underlying JS object, DOMJSProxyHandler directly subclasses
  105. * BaseProxyHandler.
  106. *
  107. * Gecko's security wrappers are examples of cross-compartment wrappers.
  108. *
  109. * ### Proxy prototype chains
  110. *
  111. * In addition to the normal methods, there are two models for proxy prototype
  112. * chains.
  113. *
  114. * 1. Proxies can use the standard prototype mechanism used throughout the
  115. * engine. To do so, simply pass a prototype to NewProxyObject() at
  116. * creation time. All prototype accesses will then "just work" to treat the
  117. * proxy as a "normal" object.
  118. *
  119. * 2. A proxy can implement more complicated prototype semantics (if, for
  120. * example, it wants to delegate the prototype lookup to a wrapped object)
  121. * by passing Proxy::LazyProto as the prototype at create time. This
  122. * guarantees that the getPrototype() handler method will be called every
  123. * time the object's prototype chain is accessed.
  124. *
  125. * This system is implemented with two methods: {get,set}Prototype. The
  126. * default implementation of setPrototype throws a TypeError. Since it is
  127. * not possible to create an object without a sense of prototype chain,
  128. * handlers must implement getPrototype if opting in to the dynamic
  129. * prototype system.
  130. */
  131. /*
  132. * BaseProxyHandler is the most generic kind of proxy handler. It does not make
  133. * any assumptions about the target. Consequently, it does not provide any
  134. * default implementation for most methods. As a convenience, a few high-level
  135. * methods, like get() and set(), are given default implementations that work by
  136. * calling the low-level methods, like getOwnPropertyDescriptor().
  137. *
  138. * Important: If you add a method here, you should probably also add a
  139. * Proxy::foo entry point with an AutoEnterPolicy. If you don't, you need an
  140. * explicit override for the method in SecurityWrapper. See bug 945826 comment 0.
  141. */
  142. class JS_FRIEND_API(BaseProxyHandler)
  143. {
  144. /*
  145. * Sometimes it's desirable to designate groups of proxy handlers as "similar".
  146. * For this, we use the notion of a "family": A consumer-provided opaque pointer
  147. * that designates the larger group to which this proxy belongs.
  148. *
  149. * If it will never be important to differentiate this proxy from others as
  150. * part of a distinct group, nullptr may be used instead.
  151. */
  152. const void* mFamily;
  153. /*
  154. * Proxy handlers can use mHasPrototype to request the following special
  155. * treatment from the JS engine:
  156. *
  157. * - When mHasPrototype is true, the engine never calls these methods:
  158. * getPropertyDescriptor, has, set, enumerate, iterate. Instead, for
  159. * these operations, it calls the "own" methods like
  160. * getOwnPropertyDescriptor, hasOwn, defineProperty,
  161. * getOwnEnumerablePropertyKeys, etc., and consults the prototype chain
  162. * if needed.
  163. *
  164. * - When mHasPrototype is true, the engine calls handler->get() only if
  165. * handler->hasOwn() says an own property exists on the proxy. If not,
  166. * it consults the prototype chain.
  167. *
  168. * This is useful because it frees the ProxyHandler from having to implement
  169. * any behavior having to do with the prototype chain.
  170. */
  171. bool mHasPrototype;
  172. /*
  173. * All proxies indicate whether they have any sort of interesting security
  174. * policy that might prevent the caller from doing something it wants to
  175. * the object. In the case of wrappers, this distinction is used to
  176. * determine whether the caller may strip off the wrapper if it so desires.
  177. */
  178. bool mHasSecurityPolicy;
  179. public:
  180. explicit constexpr BaseProxyHandler(const void* aFamily, bool aHasPrototype = false,
  181. bool aHasSecurityPolicy = false)
  182. : mFamily(aFamily),
  183. mHasPrototype(aHasPrototype),
  184. mHasSecurityPolicy(aHasSecurityPolicy)
  185. { }
  186. bool hasPrototype() const {
  187. return mHasPrototype;
  188. }
  189. bool hasSecurityPolicy() const {
  190. return mHasSecurityPolicy;
  191. }
  192. inline const void* family() const {
  193. return mFamily;
  194. }
  195. static size_t offsetOfFamily() {
  196. return offsetof(BaseProxyHandler, mFamily);
  197. }
  198. virtual bool finalizeInBackground(const Value& priv) const {
  199. /*
  200. * Called on creation of a proxy to determine whether its finalize
  201. * method can be finalized on the background thread.
  202. */
  203. return true;
  204. }
  205. virtual bool canNurseryAllocate() const {
  206. /*
  207. * Nursery allocation is allowed if and only if it is safe to not
  208. * run |finalize| when the ProxyObject dies.
  209. */
  210. return false;
  211. }
  212. /* Policy enforcement methods.
  213. *
  214. * enter() allows the policy to specify whether the caller may perform |act|
  215. * on the proxy's |id| property. In the case when |act| is CALL, |id| is
  216. * generally JSID_VOID.
  217. *
  218. * The |act| parameter to enter() specifies the action being performed.
  219. * If |bp| is false, the method suggests that the caller throw (though it
  220. * may still decide to squelch the error).
  221. *
  222. * We make these OR-able so that assertEnteredPolicy can pass a union of them.
  223. * For example, get{,Own}PropertyDescriptor is invoked by calls to ::get()
  224. * ::set(), in addition to being invoked on its own, so there are several
  225. * valid Actions that could have been entered.
  226. */
  227. typedef uint32_t Action;
  228. enum {
  229. NONE = 0x00,
  230. GET = 0x01,
  231. SET = 0x02,
  232. CALL = 0x04,
  233. ENUMERATE = 0x08,
  234. GET_PROPERTY_DESCRIPTOR = 0x10
  235. };
  236. virtual bool enter(JSContext* cx, HandleObject wrapper, HandleId id, Action act,
  237. bool* bp) const;
  238. /* Standard internal methods. */
  239. virtual bool getOwnPropertyDescriptor(JSContext* cx, HandleObject proxy, HandleId id,
  240. MutableHandle<PropertyDescriptor> desc) const = 0;
  241. virtual bool defineProperty(JSContext* cx, HandleObject proxy, HandleId id,
  242. Handle<PropertyDescriptor> desc,
  243. ObjectOpResult& result) const = 0;
  244. virtual bool ownPropertyKeys(JSContext* cx, HandleObject proxy,
  245. AutoIdVector& props) const = 0;
  246. virtual bool delete_(JSContext* cx, HandleObject proxy, HandleId id,
  247. ObjectOpResult& result) const = 0;
  248. /*
  249. * These methods are standard, but the engine does not normally call them.
  250. * They're opt-in. See "Proxy prototype chains" above.
  251. *
  252. * getPrototype() crashes if called. setPrototype() throws a TypeError.
  253. */
  254. virtual bool getPrototype(JSContext* cx, HandleObject proxy, MutableHandleObject protop) const;
  255. virtual bool setPrototype(JSContext* cx, HandleObject proxy, HandleObject proto,
  256. ObjectOpResult& result) const;
  257. /* Non-standard but conceptual kin to {g,s}etPrototype, so these live here. */
  258. virtual bool getPrototypeIfOrdinary(JSContext* cx, HandleObject proxy, bool* isOrdinary,
  259. MutableHandleObject protop) const = 0;
  260. virtual bool setImmutablePrototype(JSContext* cx, HandleObject proxy, bool* succeeded) const;
  261. virtual bool preventExtensions(JSContext* cx, HandleObject proxy,
  262. ObjectOpResult& result) const = 0;
  263. virtual bool isExtensible(JSContext* cx, HandleObject proxy, bool* extensible) const = 0;
  264. /*
  265. * These standard internal methods are implemented, as a convenience, so
  266. * that ProxyHandler subclasses don't have to provide every single method.
  267. *
  268. * The base-class implementations work by calling getPropertyDescriptor().
  269. * They do not follow any standard. When in doubt, override them.
  270. */
  271. virtual bool has(JSContext* cx, HandleObject proxy, HandleId id, bool* bp) const;
  272. virtual bool get(JSContext* cx, HandleObject proxy, HandleValue receiver,
  273. HandleId id, MutableHandleValue vp) const;
  274. virtual bool set(JSContext* cx, HandleObject proxy, HandleId id, HandleValue v,
  275. HandleValue receiver, ObjectOpResult& result) const;
  276. /*
  277. * [[Call]] and [[Construct]] are standard internal methods but according
  278. * to the spec, they are not present on every object.
  279. *
  280. * SpiderMonkey never calls a proxy's call()/construct() internal method
  281. * unless isCallable()/isConstructor() returns true for that proxy.
  282. *
  283. * BaseProxyHandler::isCallable()/isConstructor() always return false, and
  284. * BaseProxyHandler::call()/construct() crash if called. So if you're
  285. * creating a kind of that is never callable, you don't have to override
  286. * anything, but otherwise you probably want to override all four.
  287. */
  288. virtual bool call(JSContext* cx, HandleObject proxy, const CallArgs& args) const;
  289. virtual bool construct(JSContext* cx, HandleObject proxy, const CallArgs& args) const;
  290. /* SpiderMonkey extensions. */
  291. virtual bool enumerate(JSContext* cx, HandleObject proxy, MutableHandleObject objp) const;
  292. virtual bool getPropertyDescriptor(JSContext* cx, HandleObject proxy, HandleId id,
  293. MutableHandle<PropertyDescriptor> desc) const;
  294. virtual bool hasOwn(JSContext* cx, HandleObject proxy, HandleId id, bool* bp) const;
  295. virtual bool getOwnEnumerablePropertyKeys(JSContext* cx, HandleObject proxy,
  296. AutoIdVector& props) const;
  297. virtual bool nativeCall(JSContext* cx, IsAcceptableThis test, NativeImpl impl,
  298. const CallArgs& args) const;
  299. virtual bool hasInstance(JSContext* cx, HandleObject proxy, MutableHandleValue v, bool* bp) const;
  300. virtual bool getBuiltinClass(JSContext* cx, HandleObject proxy,
  301. ESClass* cls) const;
  302. virtual bool isArray(JSContext* cx, HandleObject proxy, JS::IsArrayAnswer* answer) const;
  303. virtual const char* className(JSContext* cx, HandleObject proxy) const;
  304. virtual JSString* fun_toString(JSContext* cx, HandleObject proxy, unsigned indent) const;
  305. virtual bool regexp_toShared(JSContext* cx, HandleObject proxy, RegExpGuard* g) const;
  306. virtual bool boxedValue_unbox(JSContext* cx, HandleObject proxy, MutableHandleValue vp) const;
  307. virtual void trace(JSTracer* trc, JSObject* proxy) const;
  308. virtual void finalize(JSFreeOp* fop, JSObject* proxy) const;
  309. virtual void objectMoved(JSObject* proxy, const JSObject* old) const;
  310. // Allow proxies, wrappers in particular, to specify callability at runtime.
  311. // Note: These do not take const JSObject*, but they do in spirit.
  312. // We are not prepared to do this, as there's little const correctness
  313. // in the external APIs that handle proxies.
  314. virtual bool isCallable(JSObject* obj) const;
  315. virtual bool isConstructor(JSObject* obj) const;
  316. // These two hooks must be overridden, or not overridden, in tandem -- no
  317. // overriding just one!
  318. virtual bool watch(JSContext* cx, JS::HandleObject proxy, JS::HandleId id,
  319. JS::HandleObject callable) const;
  320. virtual bool unwatch(JSContext* cx, JS::HandleObject proxy, JS::HandleId id) const;
  321. virtual bool getElements(JSContext* cx, HandleObject proxy, uint32_t begin, uint32_t end,
  322. ElementAdder* adder) const;
  323. /* See comment for weakmapKeyDelegateOp in js/Class.h. */
  324. virtual JSObject* weakmapKeyDelegate(JSObject* proxy) const;
  325. virtual bool isScripted() const { return false; }
  326. };
  327. extern JS_FRIEND_DATA(const js::Class* const) ProxyClassPtr;
  328. inline bool IsProxy(const JSObject* obj)
  329. {
  330. return GetObjectClass(obj)->isProxy();
  331. }
  332. namespace detail {
  333. const uint32_t PROXY_EXTRA_SLOTS = 2;
  334. // Layout of the values stored by a proxy. Note that API clients require the
  335. // private slot to be the first slot in the proxy's values, so that the private
  336. // slot can be accessed in the same fashion as the first reserved slot, via
  337. // {Get,Set}ReservedOrProxyPrivateSlot.
  338. struct ProxyValueArray
  339. {
  340. Value privateSlot;
  341. Value extraSlots[PROXY_EXTRA_SLOTS];
  342. ProxyValueArray()
  343. : privateSlot(JS::UndefinedValue())
  344. {
  345. for (size_t i = 0; i < PROXY_EXTRA_SLOTS; i++)
  346. extraSlots[i] = JS::UndefinedValue();
  347. }
  348. };
  349. // All proxies share the same data layout. Following the object's shape and
  350. // type, the proxy has a ProxyDataLayout structure with a pointer to an array
  351. // of values and the proxy's handler. This is designed both so that proxies can
  352. // be easily swapped with other objects (via RemapWrapper) and to mimic the
  353. // layout of other objects (proxies and other objects have the same size) so
  354. // that common code can access either type of object.
  355. //
  356. // See GetReservedOrProxyPrivateSlot below.
  357. struct ProxyDataLayout
  358. {
  359. ProxyValueArray* values;
  360. const BaseProxyHandler* handler;
  361. };
  362. const uint32_t ProxyDataOffset = 2 * sizeof(void*);
  363. inline ProxyDataLayout*
  364. GetProxyDataLayout(JSObject* obj)
  365. {
  366. MOZ_ASSERT(IsProxy(obj));
  367. return reinterpret_cast<ProxyDataLayout*>(reinterpret_cast<uint8_t*>(obj) + ProxyDataOffset);
  368. }
  369. inline const ProxyDataLayout*
  370. GetProxyDataLayout(const JSObject* obj)
  371. {
  372. MOZ_ASSERT(IsProxy(obj));
  373. return reinterpret_cast<const ProxyDataLayout*>(reinterpret_cast<const uint8_t*>(obj) +
  374. ProxyDataOffset);
  375. }
  376. } // namespace detail
  377. inline const BaseProxyHandler*
  378. GetProxyHandler(const JSObject* obj)
  379. {
  380. return detail::GetProxyDataLayout(obj)->handler;
  381. }
  382. inline const Value&
  383. GetProxyPrivate(const JSObject* obj)
  384. {
  385. return detail::GetProxyDataLayout(obj)->values->privateSlot;
  386. }
  387. inline JSObject*
  388. GetProxyTargetObject(JSObject* obj)
  389. {
  390. return GetProxyPrivate(obj).toObjectOrNull();
  391. }
  392. inline const Value&
  393. GetProxyExtra(const JSObject* obj, size_t n)
  394. {
  395. MOZ_ASSERT(n < detail::PROXY_EXTRA_SLOTS);
  396. return detail::GetProxyDataLayout(obj)->values->extraSlots[n];
  397. }
  398. inline void
  399. SetProxyHandler(JSObject* obj, const BaseProxyHandler* handler)
  400. {
  401. detail::GetProxyDataLayout(obj)->handler = handler;
  402. }
  403. JS_FRIEND_API(void)
  404. SetValueInProxy(Value* slot, const Value& value);
  405. inline void
  406. SetProxyExtra(JSObject* obj, size_t n, const Value& extra)
  407. {
  408. MOZ_ASSERT(n < detail::PROXY_EXTRA_SLOTS);
  409. Value* vp = &detail::GetProxyDataLayout(obj)->values->extraSlots[n];
  410. // Trigger a barrier before writing the slot.
  411. if (vp->isMarkable() || extra.isMarkable())
  412. SetValueInProxy(vp, extra);
  413. else
  414. *vp = extra;
  415. }
  416. inline bool
  417. IsScriptedProxy(const JSObject* obj)
  418. {
  419. return IsProxy(obj) && GetProxyHandler(obj)->isScripted();
  420. }
  421. inline const Value&
  422. GetReservedOrProxyPrivateSlot(const JSObject* obj, size_t slot)
  423. {
  424. MOZ_ASSERT(slot == 0);
  425. MOZ_ASSERT(slot < JSCLASS_RESERVED_SLOTS(GetObjectClass(obj)) || IsProxy(obj));
  426. return reinterpret_cast<const shadow::Object*>(obj)->slotRef(slot);
  427. }
  428. inline void
  429. SetReservedOrProxyPrivateSlot(JSObject* obj, size_t slot, const Value& value)
  430. {
  431. MOZ_ASSERT(slot == 0);
  432. MOZ_ASSERT(slot < JSCLASS_RESERVED_SLOTS(GetObjectClass(obj)) || IsProxy(obj));
  433. shadow::Object* sobj = reinterpret_cast<shadow::Object*>(obj);
  434. if (sobj->slotRef(slot).isMarkable() || value.isMarkable())
  435. SetReservedOrProxyPrivateSlotWithBarrier(obj, slot, value);
  436. else
  437. sobj->slotRef(slot) = value;
  438. }
  439. class MOZ_STACK_CLASS ProxyOptions {
  440. protected:
  441. /* protected constructor for subclass */
  442. explicit ProxyOptions(bool singletonArg, bool lazyProtoArg = false)
  443. : singleton_(singletonArg),
  444. lazyProto_(lazyProtoArg),
  445. clasp_(ProxyClassPtr)
  446. {}
  447. public:
  448. ProxyOptions() : singleton_(false),
  449. lazyProto_(false),
  450. clasp_(ProxyClassPtr)
  451. {}
  452. bool singleton() const { return singleton_; }
  453. ProxyOptions& setSingleton(bool flag) {
  454. singleton_ = flag;
  455. return *this;
  456. }
  457. bool lazyProto() const { return lazyProto_; }
  458. ProxyOptions& setLazyProto(bool flag) {
  459. lazyProto_ = flag;
  460. return *this;
  461. }
  462. const Class* clasp() const {
  463. return clasp_;
  464. }
  465. ProxyOptions& setClass(const Class* claspArg) {
  466. clasp_ = claspArg;
  467. return *this;
  468. }
  469. private:
  470. bool singleton_;
  471. bool lazyProto_;
  472. const Class* clasp_;
  473. };
  474. JS_FRIEND_API(JSObject*)
  475. NewProxyObject(JSContext* cx, const BaseProxyHandler* handler, HandleValue priv,
  476. JSObject* proto, const ProxyOptions& options = ProxyOptions());
  477. JSObject*
  478. RenewProxyObject(JSContext* cx, JSObject* obj, BaseProxyHandler* handler, const Value& priv);
  479. class JS_FRIEND_API(AutoEnterPolicy)
  480. {
  481. public:
  482. typedef BaseProxyHandler::Action Action;
  483. AutoEnterPolicy(JSContext* cx, const BaseProxyHandler* handler,
  484. HandleObject wrapper, HandleId id, Action act, bool mayThrow)
  485. #ifdef JS_DEBUG
  486. : context(nullptr)
  487. #endif
  488. {
  489. allow = handler->hasSecurityPolicy() ? handler->enter(cx, wrapper, id, act, &rv)
  490. : true;
  491. recordEnter(cx, wrapper, id, act);
  492. // We want to throw an exception if all of the following are true:
  493. // * The policy disallowed access.
  494. // * The policy set rv to false, indicating that we should throw.
  495. // * The caller did not instruct us to ignore exceptions.
  496. // * The policy did not throw itself.
  497. if (!allow && !rv && mayThrow)
  498. reportErrorIfExceptionIsNotPending(cx, id);
  499. }
  500. virtual ~AutoEnterPolicy() { recordLeave(); }
  501. inline bool allowed() { return allow; }
  502. inline bool returnValue() { MOZ_ASSERT(!allowed()); return rv; }
  503. protected:
  504. // no-op constructor for subclass
  505. AutoEnterPolicy()
  506. #ifdef JS_DEBUG
  507. : context(nullptr)
  508. , enteredAction(BaseProxyHandler::NONE)
  509. #endif
  510. {}
  511. void reportErrorIfExceptionIsNotPending(JSContext* cx, jsid id);
  512. bool allow;
  513. bool rv;
  514. #ifdef JS_DEBUG
  515. JSContext* context;
  516. mozilla::Maybe<HandleObject> enteredProxy;
  517. mozilla::Maybe<HandleId> enteredId;
  518. Action enteredAction;
  519. // NB: We explicitly don't track the entered action here, because sometimes
  520. // set() methods do an implicit get() during their implementation, leading
  521. // to spurious assertions.
  522. AutoEnterPolicy* prev;
  523. void recordEnter(JSContext* cx, HandleObject proxy, HandleId id, Action act);
  524. void recordLeave();
  525. friend JS_FRIEND_API(void) assertEnteredPolicy(JSContext* cx, JSObject* proxy, jsid id, Action act);
  526. #else
  527. inline void recordEnter(JSContext* cx, JSObject* proxy, jsid id, Action act) {}
  528. inline void recordLeave() {}
  529. #endif
  530. };
  531. #ifdef JS_DEBUG
  532. class JS_FRIEND_API(AutoWaivePolicy) : public AutoEnterPolicy {
  533. public:
  534. AutoWaivePolicy(JSContext* cx, HandleObject proxy, HandleId id,
  535. BaseProxyHandler::Action act)
  536. {
  537. allow = true;
  538. recordEnter(cx, proxy, id, act);
  539. }
  540. };
  541. #else
  542. class JS_FRIEND_API(AutoWaivePolicy) {
  543. public:
  544. AutoWaivePolicy(JSContext* cx, HandleObject proxy, HandleId id,
  545. BaseProxyHandler::Action act)
  546. {}
  547. };
  548. #endif
  549. #ifdef JS_DEBUG
  550. extern JS_FRIEND_API(void)
  551. assertEnteredPolicy(JSContext* cx, JSObject* obj, jsid id,
  552. BaseProxyHandler::Action act);
  553. #else
  554. inline void assertEnteredPolicy(JSContext* cx, JSObject* obj, jsid id,
  555. BaseProxyHandler::Action act)
  556. {}
  557. #endif
  558. extern JS_FRIEND_API(JSObject*)
  559. InitProxyClass(JSContext* cx, JS::HandleObject obj);
  560. } /* namespace js */
  561. #endif /* js_Proxy_h */