StructuredClone.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  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
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef js_StructuredClone_h
  6. #define js_StructuredClone_h
  7. #include "mozilla/Attributes.h"
  8. #include "mozilla/BufferList.h"
  9. #include "mozilla/Move.h"
  10. #include <stdint.h>
  11. #include "jstypes.h"
  12. #include "js/RootingAPI.h"
  13. #include "js/TypeDecls.h"
  14. #include "js/Value.h"
  15. struct JSRuntime;
  16. struct JSStructuredCloneReader;
  17. struct JSStructuredCloneWriter;
  18. // API for the HTML5 internal structured cloning algorithm.
  19. namespace JS {
  20. enum class StructuredCloneScope : uint32_t {
  21. SameProcessSameThread,
  22. SameProcessDifferentThread,
  23. /**
  24. * When writing, this means we're writing for an audience in a different
  25. * process. Produce serialized data that can be sent to other processes,
  26. * bitwise copied, or even stored as bytes in a database and read by later
  27. * versions of Firefox years from now. The HTML5 spec refers to this as
  28. * "ForStorage" as in StructuredSerializeForStorage, though we use
  29. * DifferentProcess for IPC as well as storage.
  30. *
  31. * Transferable objects are limited to ArrayBuffers, whose contents are
  32. * copied into the serialized data (rather than just writing a pointer).
  33. */
  34. DifferentProcess,
  35. /**
  36. * Handle a backwards-compatibility case with IndexedDB (bug 1434308): when
  37. * reading, this means to treat legacy SameProcessSameThread data as if it
  38. * were DifferentProcess.
  39. *
  40. * Do not use this for writing; use DifferentProcess instead.
  41. */
  42. DifferentProcessForIndexedDB,
  43. /**
  44. * Existing code wants to be able to create an uninitialized
  45. * JSStructuredCloneData without knowing the scope, then populate it with
  46. * data (at which point the scope *is* known.)
  47. */
  48. Unassigned
  49. };
  50. enum TransferableOwnership {
  51. /** Transferable data has not been filled in yet */
  52. SCTAG_TMO_UNFILLED = 0,
  53. /** Structured clone buffer does not yet own the data */
  54. SCTAG_TMO_UNOWNED = 1,
  55. /** All values at least this large are owned by the clone buffer */
  56. SCTAG_TMO_FIRST_OWNED = 2,
  57. /** Data is a pointer that can be freed */
  58. SCTAG_TMO_ALLOC_DATA = 2,
  59. /** Data is a memory mapped pointer */
  60. SCTAG_TMO_MAPPED_DATA = 3,
  61. /**
  62. * Data is embedding-specific. The engine can free it by calling the
  63. * freeTransfer op. The embedding can also use SCTAG_TMO_USER_MIN and
  64. * greater, up to 32 bits, to distinguish specific ownership variants.
  65. */
  66. SCTAG_TMO_CUSTOM = 4,
  67. SCTAG_TMO_USER_MIN
  68. };
  69. class CloneDataPolicy
  70. {
  71. bool sharedArrayBuffer_;
  72. public:
  73. // The default is to allow all policy-controlled aspects.
  74. CloneDataPolicy() :
  75. sharedArrayBuffer_(true)
  76. {}
  77. // In the JS engine, SharedArrayBuffers can only be cloned intra-process
  78. // because the shared memory areas are allocated in process-private memory.
  79. // Clients should therefore deny SharedArrayBuffers when cloning data that
  80. // are to be transmitted inter-process.
  81. //
  82. // Clients should also deny SharedArrayBuffers when cloning data that are to
  83. // be transmitted intra-process if policy needs dictate such denial.
  84. CloneDataPolicy& denySharedArrayBuffer() {
  85. sharedArrayBuffer_ = false;
  86. return *this;
  87. }
  88. bool isSharedArrayBufferAllowed() const {
  89. return sharedArrayBuffer_;
  90. }
  91. };
  92. } /* namespace JS */
  93. namespace js {
  94. template <typename T, typename AllocPolicy> struct BufferIterator;
  95. }
  96. /**
  97. * Read structured data from the reader r. This hook is used to read a value
  98. * previously serialized by a call to the WriteStructuredCloneOp hook.
  99. *
  100. * tag and data are the pair of uint32_t values from the header. The callback
  101. * may use the JS_Read* APIs to read any other relevant parts of the object
  102. * from the reader r. closure is any value passed to the JS_ReadStructuredClone
  103. * function. Return the new object on success, nullptr on error/exception.
  104. */
  105. typedef JSObject* (*ReadStructuredCloneOp)(JSContext* cx, JSStructuredCloneReader* r,
  106. uint32_t tag, uint32_t data, void* closure);
  107. /**
  108. * Structured data serialization hook. The engine can write primitive values,
  109. * Objects, Arrays, Dates, RegExps, TypedArrays, ArrayBuffers, Sets, Maps,
  110. * and SharedTypedArrays. Any other type of object requires application support.
  111. * This callback must first use the JS_WriteUint32Pair API to write an object
  112. * header, passing a value greater than JS_SCTAG_USER to the tag parameter.
  113. * Then it can use the JS_Write* APIs to write any other relevant parts of
  114. * the value v to the writer w. closure is any value passed to the
  115. * JS_WriteStructuredClone function.
  116. *
  117. * Return true on success, false on error/exception.
  118. */
  119. typedef bool (*WriteStructuredCloneOp)(JSContext* cx, JSStructuredCloneWriter* w,
  120. JS::HandleObject obj, void* closure);
  121. /**
  122. * This is called when JS_WriteStructuredClone is given an invalid transferable.
  123. * To follow HTML5, the application must throw a DATA_CLONE_ERR DOMException
  124. * with error set to one of the JS_SCERR_* values.
  125. */
  126. typedef void (*StructuredCloneErrorOp)(JSContext* cx, uint32_t errorid);
  127. /**
  128. * This is called when JS_ReadStructuredClone receives a transferable object
  129. * not known to the engine. If this hook does not exist or returns false, the
  130. * JS engine calls the reportError op if set, otherwise it throws a
  131. * DATA_CLONE_ERR DOM Exception. This method is called before any other
  132. * callback and must return a non-null object in returnObject on success.
  133. */
  134. typedef bool (*ReadTransferStructuredCloneOp)(JSContext* cx, JSStructuredCloneReader* r,
  135. uint32_t tag, void* content, uint64_t extraData,
  136. void* closure,
  137. JS::MutableHandleObject returnObject);
  138. /**
  139. * Called when JS_WriteStructuredClone receives a transferable object not
  140. * handled by the engine. If this hook does not exist or returns false, the JS
  141. * engine will call the reportError hook or fall back to throwing a
  142. * DATA_CLONE_ERR DOM Exception. This method is called before any other
  143. * callback.
  144. *
  145. * tag: indicates what type of transferable this is. Must be greater than
  146. * 0xFFFF0201 (value of the internal SCTAG_TRANSFER_MAP_PENDING_ENTRY)
  147. *
  148. * ownership: see TransferableOwnership, above. Used to communicate any needed
  149. * ownership info to the FreeTransferStructuredCloneOp.
  150. *
  151. * content, extraData: what the ReadTransferStructuredCloneOp will receive
  152. */
  153. typedef bool (*TransferStructuredCloneOp)(JSContext* cx,
  154. JS::Handle<JSObject*> obj,
  155. void* closure,
  156. // Output:
  157. uint32_t* tag,
  158. JS::TransferableOwnership* ownership,
  159. void** content,
  160. uint64_t* extraData);
  161. /**
  162. * Called when freeing an unknown transferable object. Note that it
  163. * should never trigger a garbage collection (and will assert in a
  164. * debug build if it does.)
  165. */
  166. typedef void (*FreeTransferStructuredCloneOp)(uint32_t tag, JS::TransferableOwnership ownership,
  167. void* content, uint64_t extraData, void* closure);
  168. // The maximum supported structured-clone serialization format version.
  169. // Increment this when anything at all changes in the serialization format.
  170. // (Note that this does not need to be bumped for Transferable-only changes,
  171. // since they are never saved to persistent storage.)
  172. #define JS_STRUCTURED_CLONE_VERSION 8
  173. struct JSStructuredCloneCallbacks {
  174. ReadStructuredCloneOp read;
  175. WriteStructuredCloneOp write;
  176. StructuredCloneErrorOp reportError;
  177. ReadTransferStructuredCloneOp readTransfer;
  178. TransferStructuredCloneOp writeTransfer;
  179. FreeTransferStructuredCloneOp freeTransfer;
  180. };
  181. enum OwnTransferablePolicy {
  182. OwnsTransferablesIfAny,
  183. IgnoreTransferablesIfAny,
  184. NoTransferables
  185. };
  186. /**
  187. * JSStructuredCloneData represents structured clone data together with the
  188. * information needed to read/write/transfer/free the records within it, in the
  189. * form of a set of callbacks.
  190. */
  191. class MOZ_NON_MEMMOVABLE JS_PUBLIC_API(JSStructuredCloneData) {
  192. public:
  193. using BufferList = mozilla::BufferList<js::SystemAllocPolicy>;
  194. using Iterator = BufferList::IterImpl;
  195. private:
  196. static const size_t kStandardCapacity = 4096;
  197. BufferList bufList_;
  198. // The (address space, thread) scope within which this clone is valid. Note
  199. // that this must be either set during construction, or start out as
  200. // Unassigned and transition once to something else.
  201. JS::StructuredCloneScope scope_;
  202. const JSStructuredCloneCallbacks* callbacks_;
  203. void* closure_;
  204. OwnTransferablePolicy ownTransferables_;
  205. friend struct JSStructuredCloneWriter;
  206. friend class JS_PUBLIC_API(JSAutoStructuredCloneBuffer);
  207. template <typename T, typename AllocPolicy> friend struct js::BufferIterator;
  208. public:
  209. // The constructor must be infallible but SystemAllocPolicy is not, so both
  210. // the initial size and initial capacity of the BufferList must be zero.
  211. explicit JSStructuredCloneData(JS::StructuredCloneScope aScope)
  212. : bufList_(0, 0, kStandardCapacity, js::SystemAllocPolicy())
  213. , scope_(aScope)
  214. , callbacks_(nullptr)
  215. , closure_(nullptr)
  216. , ownTransferables_(OwnTransferablePolicy::NoTransferables)
  217. {}
  218. // Steal the raw data from a BufferList. In this case, we don't know the
  219. // scope and none of the callback info is assigned yet.
  220. JSStructuredCloneData(BufferList&& buffers, JS::StructuredCloneScope aScope)
  221. : bufList_(mozilla::Move(buffers))
  222. , scope_(aScope)
  223. , callbacks_(nullptr)
  224. , closure_(nullptr)
  225. , ownTransferables_(OwnTransferablePolicy::NoTransferables)
  226. {}
  227. MOZ_IMPLICIT JSStructuredCloneData(BufferList&& buffers)
  228. : JSStructuredCloneData(mozilla::Move(buffers), JS::StructuredCloneScope::Unassigned)
  229. {}
  230. JSStructuredCloneData(JSStructuredCloneData&& other) = default;
  231. JSStructuredCloneData& operator=(JSStructuredCloneData&& other) = default;
  232. ~JSStructuredCloneData() { discardTransferables(); }
  233. void setCallbacks(const JSStructuredCloneCallbacks* callbacks,
  234. void* closure,
  235. OwnTransferablePolicy policy)
  236. {
  237. callbacks_ = callbacks;
  238. closure_ = closure;
  239. ownTransferables_ = policy;
  240. }
  241. JS::StructuredCloneScope scope() const { return scope_; }
  242. void initScope(JS::StructuredCloneScope aScope) {
  243. MOZ_ASSERT(Size() == 0, "initScope() of nonempty JSStructuredCloneData");
  244. if (scope_ != JS::StructuredCloneScope::Unassigned)
  245. MOZ_ASSERT(scope_ == aScope, "Cannot change scope after it has been initialized");
  246. scope_ = aScope;
  247. }
  248. size_t Size() const { return bufList_.Size(); }
  249. const Iterator Start() const { return bufList_.Iter(); }
  250. bool Advance(Iterator& iter, size_t distance) const {
  251. return iter.AdvanceAcrossSegments(bufList_, distance);
  252. }
  253. bool ReadBytes(Iterator& iter, char* buffer, size_t size) const {
  254. return bufList_.ReadBytes(iter, buffer, size);
  255. }
  256. // Append new data to the end of the buffer.
  257. bool AppendBytes(const char* data, size_t size) {
  258. MOZ_ASSERT(scope_ != JS::StructuredCloneScope::Unassigned);
  259. return bufList_.WriteBytes(data, size);
  260. }
  261. // Update data stored within the existing buffer. There must be at least
  262. // 'size' bytes between the position of 'iter' and the end of the buffer.
  263. bool UpdateBytes(Iterator& iter, const char* data, size_t size) const {
  264. MOZ_ASSERT(scope_ != JS::StructuredCloneScope::Unassigned);
  265. while (size > 0) {
  266. size_t remaining = iter.RemainingInSegment();
  267. size_t nbytes = std::min(remaining, size);
  268. memcpy(iter.Data(), data, nbytes);
  269. data += nbytes;
  270. size -= nbytes;
  271. iter.Advance(bufList_, nbytes);
  272. }
  273. return true;
  274. }
  275. void Clear() {
  276. discardTransferables();
  277. bufList_.Clear();
  278. }
  279. // Return a new read-only JSStructuredCloneData that "borrows" the contents
  280. // of |this|. Its lifetime should not exceed the donor's. This is only
  281. // allowed for DifferentProcess clones, so finalization of the borrowing
  282. // clone will do nothing.
  283. JSStructuredCloneData Borrow(Iterator& iter, size_t size, bool* success) const
  284. {
  285. MOZ_ASSERT(scope_ == JS::StructuredCloneScope::DifferentProcess);
  286. return JSStructuredCloneData(bufList_.Borrow<js::SystemAllocPolicy>(iter, size, success),
  287. scope_);
  288. }
  289. // Iterate over all contained data, one BufferList segment's worth at a
  290. // time, and invoke the given FunctionToApply with the data pointer and
  291. // size. The function should return a bool value, and this loop will exit
  292. // with false if the function ever returns false.
  293. template <typename FunctionToApply>
  294. bool ForEachDataChunk(FunctionToApply&& function) const {
  295. Iterator iter = bufList_.Iter();
  296. while (!iter.Done()) {
  297. if (!function(iter.Data(), iter.RemainingInSegment()))
  298. return false;
  299. iter.Advance(bufList_, iter.RemainingInSegment());
  300. }
  301. return true;
  302. }
  303. // Append the entire contents of other's bufList_ to our own.
  304. bool Append(const JSStructuredCloneData& other) {
  305. MOZ_ASSERT(scope_ == other.scope_);
  306. return other.ForEachDataChunk([&](const char* data, size_t size) {
  307. return AppendBytes(data, size);
  308. });
  309. }
  310. void discardTransferables();
  311. };
  312. /** Note: if the *data contains transferable objects, it can be read only once. */
  313. JS_PUBLIC_API(bool)
  314. JS_ReadStructuredClone(JSContext* cx, JSStructuredCloneData& data, uint32_t version,
  315. JS::StructuredCloneScope scope,
  316. JS::MutableHandleValue vp,
  317. const JSStructuredCloneCallbacks* optionalCallbacks, void* closure);
  318. JS_PUBLIC_API(bool)
  319. JS_WriteStructuredClone(JSContext* cx, JS::HandleValue v, JSStructuredCloneData* data,
  320. JS::StructuredCloneScope scope,
  321. JS::CloneDataPolicy cloneDataPolicy,
  322. const JSStructuredCloneCallbacks* optionalCallbacks,
  323. void* closure, JS::HandleValue transferable);
  324. JS_PUBLIC_API(bool)
  325. JS_StructuredCloneHasTransferables(JSStructuredCloneData& data, bool* hasTransferable);
  326. JS_PUBLIC_API(bool)
  327. JS_StructuredClone(JSContext* cx, JS::HandleValue v, JS::MutableHandleValue vp,
  328. const JSStructuredCloneCallbacks* optionalCallbacks, void* closure);
  329. /**
  330. * The C-style API calls to read and write structured clones are fragile --
  331. * they rely on the caller to properly handle ownership of the clone data, and
  332. * the handling of the input data as well as the interpretation of the contents
  333. * of the clone buffer are dependent on the callbacks passed in. If you
  334. * serialize and deserialize with different callbacks, the results are
  335. * questionable.
  336. *
  337. * JSAutoStructuredCloneBuffer wraps things up in an RAII class for data
  338. * management, and uses the same callbacks for both writing and reading
  339. * (serializing and deserializing).
  340. */
  341. class JS_PUBLIC_API(JSAutoStructuredCloneBuffer) {
  342. const JS::StructuredCloneScope scope_;
  343. JSStructuredCloneData data_;
  344. uint32_t version_;
  345. public:
  346. JSAutoStructuredCloneBuffer(JS::StructuredCloneScope aScope,
  347. const JSStructuredCloneCallbacks* callbacks, void* closure)
  348. : scope_(aScope), data_(aScope), version_(JS_STRUCTURED_CLONE_VERSION)
  349. {
  350. data_.setCallbacks(callbacks, closure, OwnTransferablePolicy::NoTransferables);
  351. }
  352. JSAutoStructuredCloneBuffer(JSAutoStructuredCloneBuffer&& other);
  353. JSAutoStructuredCloneBuffer& operator=(JSAutoStructuredCloneBuffer&& other);
  354. ~JSAutoStructuredCloneBuffer() { clear(); }
  355. JSStructuredCloneData& data() { return data_; }
  356. bool empty() const { return !data_.Size(); }
  357. void clear();
  358. JS::StructuredCloneScope scope() const { return scope_; }
  359. /**
  360. * Adopt some memory. It will be automatically freed by the destructor.
  361. * data must have been allocated by the JS engine (e.g., extracted via
  362. * JSAutoStructuredCloneBuffer::steal).
  363. */
  364. void adopt(JSStructuredCloneData&& data, uint32_t version=JS_STRUCTURED_CLONE_VERSION,
  365. const JSStructuredCloneCallbacks* callbacks=nullptr, void* closure=nullptr);
  366. /**
  367. * Release the buffer and transfer ownership to the caller.
  368. */
  369. void steal(JSStructuredCloneData* data, uint32_t* versionp=nullptr,
  370. const JSStructuredCloneCallbacks** callbacks=nullptr, void** closure=nullptr);
  371. /**
  372. * Abandon ownership of any transferable objects stored in the buffer,
  373. * without freeing the buffer itself. Useful when copying the data out into
  374. * an external container, though note that you will need to use adopt() to
  375. * properly release that data eventually.
  376. */
  377. void abandon() { data_.ownTransferables_ = OwnTransferablePolicy::IgnoreTransferablesIfAny; }
  378. bool read(JSContext* cx, JS::MutableHandleValue vp,
  379. const JSStructuredCloneCallbacks* optionalCallbacks=nullptr, void* closure=nullptr);
  380. bool write(JSContext* cx, JS::HandleValue v,
  381. const JSStructuredCloneCallbacks* optionalCallbacks=nullptr, void* closure=nullptr);
  382. bool write(JSContext* cx, JS::HandleValue v, JS::HandleValue transferable,
  383. JS::CloneDataPolicy cloneDataPolicy,
  384. const JSStructuredCloneCallbacks* optionalCallbacks=nullptr, void* closure=nullptr);
  385. private:
  386. // Copy and assignment are not supported.
  387. JSAutoStructuredCloneBuffer(const JSAutoStructuredCloneBuffer& other) = delete;
  388. JSAutoStructuredCloneBuffer& operator=(const JSAutoStructuredCloneBuffer& other) = delete;
  389. };
  390. // The range of tag values the application may use for its own custom object types.
  391. #define JS_SCTAG_USER_MIN ((uint32_t) 0xFFFF8000)
  392. #define JS_SCTAG_USER_MAX ((uint32_t) 0xFFFFFFFF)
  393. #define JS_SCERR_RECURSION 0
  394. #define JS_SCERR_TRANSFERABLE 1
  395. #define JS_SCERR_DUP_TRANSFERABLE 2
  396. #define JS_SCERR_UNSUPPORTED_TYPE 3
  397. JS_PUBLIC_API(bool)
  398. JS_ReadUint32Pair(JSStructuredCloneReader* r, uint32_t* p1, uint32_t* p2);
  399. JS_PUBLIC_API(bool)
  400. JS_ReadBytes(JSStructuredCloneReader* r, void* p, size_t len);
  401. JS_PUBLIC_API(bool)
  402. JS_ReadTypedArray(JSStructuredCloneReader* r, JS::MutableHandleValue vp);
  403. JS_PUBLIC_API(bool)
  404. JS_WriteUint32Pair(JSStructuredCloneWriter* w, uint32_t tag, uint32_t data);
  405. JS_PUBLIC_API(bool)
  406. JS_WriteBytes(JSStructuredCloneWriter* w, const void* p, size_t len);
  407. JS_PUBLIC_API(bool)
  408. JS_WriteString(JSStructuredCloneWriter* w, JS::HandleString str);
  409. JS_PUBLIC_API(bool)
  410. JS_WriteTypedArray(JSStructuredCloneWriter* w, JS::HandleValue v);
  411. JS_PUBLIC_API(bool)
  412. JS_ObjectNotWritten(JSStructuredCloneWriter* w, JS::HandleObject obj);
  413. JS_PUBLIC_API(JS::StructuredCloneScope)
  414. JS_GetStructuredCloneScope(JSStructuredCloneWriter* w);
  415. #endif /* js_StructuredClone_h */