StructuredSerialize.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright (c) 2022, Daniel Ehrenberg <dan@littledan.dev>
  3. * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
  4. * Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/StdLibExtras.h>
  9. #include <AK/String.h>
  10. #include <AK/Vector.h>
  11. #include <LibJS/Forward.h>
  12. #include <LibJS/Runtime/BigInt.h>
  13. #include <LibJS/Runtime/BooleanObject.h>
  14. #include <LibJS/Runtime/Date.h>
  15. #include <LibJS/Runtime/NumberObject.h>
  16. #include <LibJS/Runtime/PrimitiveString.h>
  17. #include <LibJS/Runtime/StringObject.h>
  18. #include <LibJS/Runtime/VM.h>
  19. #include <LibWeb/Bindings/ExceptionOrUtils.h>
  20. #include <LibWeb/HTML/StructuredSerialize.h>
  21. #include <LibWeb/WebIDL/ExceptionOr.h>
  22. namespace Web::HTML {
  23. // Binary format:
  24. // A list of adjacent shallow values, which may contain references to other
  25. // values (noted by their position in the list, one value following another).
  26. // This list represents the "memory" in the StructuredSerialize algorithm.
  27. // The first item in the list is the root, i.e., the value of everything.
  28. // The format is generally u32-aligned (hence this leaking out into the type)
  29. // Each value has a length based on its type, as defined below.
  30. //
  31. // (Should more redundancy be added, e.g., for lengths/positions of values?)
  32. enum ValueTag {
  33. // Unused, for ease of catching bugs.
  34. Empty,
  35. // UndefinedPrimitive is serialized indicating that the Type is Undefined, no value is serialized.
  36. UndefinedPrimitive,
  37. // NullPrimitive is serialized indicating that the Type is Null, no value is serialized.
  38. NullPrimitive,
  39. // Following u32 is the boolean value.
  40. BooleanPrimitive,
  41. // Following two u32s are the double value.
  42. NumberPrimitive,
  43. // The BigIntPrimitive is serialized as a string in base 10 representation.
  44. // Following two u32s representing the length of the string, then the following u32s, equal to size, is the string representation.
  45. BigIntPrimitive,
  46. // Following two u32s representing the length of the string, then the following u32s, equal to size, is the string representation.
  47. StringPrimitive,
  48. BooleanObject,
  49. NumberObject,
  50. StringObject,
  51. DateObject,
  52. // TODO: Define many more types
  53. // This tag or higher are understood to be errors
  54. ValueTagMax,
  55. };
  56. // Serializing and deserializing are each two passes:
  57. // 1. Fill up the memory with all the values, but without translating references
  58. // 2. Translate all the references into the appropriate form
  59. class Serializer {
  60. public:
  61. Serializer(JS::VM& vm)
  62. : m_vm(vm)
  63. {
  64. }
  65. WebIDL::ExceptionOr<void> serialize(JS::Value value)
  66. {
  67. if (value.is_undefined()) {
  68. m_serialized.append(ValueTag::UndefinedPrimitive);
  69. } else if (value.is_null()) {
  70. m_serialized.append(ValueTag::NullPrimitive);
  71. } else if (value.is_boolean()) {
  72. m_serialized.append(ValueTag::BooleanPrimitive);
  73. m_serialized.append(static_cast<u32>(value.as_bool()));
  74. } else if (value.is_number()) {
  75. m_serialized.append(ValueTag::NumberPrimitive);
  76. double number = value.as_double();
  77. m_serialized.append(bit_cast<u32*>(&number), 2);
  78. } else if (value.is_bigint()) {
  79. m_serialized.append(ValueTag::BigIntPrimitive);
  80. auto& val = value.as_bigint();
  81. TRY(serialize_string(m_serialized, TRY_OR_THROW_OOM(m_vm, val.to_string())));
  82. } else if (value.is_string()) {
  83. m_serialized.append(ValueTag::StringPrimitive);
  84. TRY(serialize_string(m_serialized, value.as_string()));
  85. } else if (value.is_object() && is<JS::BooleanObject>(value.as_object())) {
  86. m_serialized.append(ValueTag::BooleanObject);
  87. auto& boolean_object = static_cast<JS::BooleanObject&>(value.as_object());
  88. m_serialized.append(bit_cast<u32>(static_cast<u32>(boolean_object.boolean())));
  89. } else if (value.is_object() && is<JS::NumberObject>(value.as_object())) {
  90. m_serialized.append(ValueTag::NumberObject);
  91. auto& number_object = static_cast<JS::NumberObject&>(value.as_object());
  92. double const number = number_object.number();
  93. m_serialized.append(bit_cast<u32*>(&number), 2);
  94. } else if (value.is_object() && is<JS::StringObject>(value.as_object())) {
  95. m_serialized.append(ValueTag::StringObject);
  96. auto& string_object = static_cast<JS::StringObject&>(value.as_object());
  97. TRY(serialize_string(m_serialized, string_object.primitive_string()));
  98. } else if (value.is_object() && is<JS::Date>(value.as_object())) {
  99. m_serialized.append(ValueTag::DateObject);
  100. auto& date_object = static_cast<JS::Date&>(value.as_object());
  101. double const date_value = date_object.date_value();
  102. m_serialized.append(bit_cast<u32*>(&date_value), 2);
  103. } else {
  104. // TODO: Define many more types
  105. m_error = "Unsupported type"sv;
  106. }
  107. // Second pass: Update the objects to point to other objects in memory
  108. return {};
  109. }
  110. WebIDL::ExceptionOr<Vector<u32>> result()
  111. {
  112. if (m_error.is_null())
  113. return m_serialized;
  114. return throw_completion(WebIDL::DataCloneError::create(*m_vm.current_realm(), m_error));
  115. }
  116. private:
  117. AK::StringView m_error;
  118. SerializationMemory m_memory; // JS value -> index
  119. SerializationRecord m_serialized;
  120. JS::VM& m_vm;
  121. WebIDL::ExceptionOr<void> serialize_string(Vector<u32>& vector, String const& string)
  122. {
  123. u64 const size = string.code_points().byte_length();
  124. // Append size of the string to the serialized structure.
  125. TRY_OR_THROW_OOM(m_vm, vector.try_append(bit_cast<u32*>(&size), 2));
  126. // Append the bytes of the string to the serialized structure.
  127. u64 byte_position = 0;
  128. ReadonlyBytes const bytes = { string.code_points().bytes(), string.code_points().byte_length() };
  129. while (byte_position < size) {
  130. u32 combined_value = 0;
  131. for (u8 i = 0; i < 4; ++i) {
  132. u8 const byte = bytes[byte_position];
  133. combined_value |= byte << (i * 8);
  134. byte_position++;
  135. if (byte_position == size)
  136. break;
  137. }
  138. TRY_OR_THROW_OOM(m_vm, vector.try_append(combined_value));
  139. }
  140. return {};
  141. }
  142. WebIDL::ExceptionOr<void> serialize_string(Vector<u32>& vector, JS::PrimitiveString const& primitive_string)
  143. {
  144. auto string = primitive_string.utf8_string();
  145. TRY(serialize_string(vector, string));
  146. return {};
  147. }
  148. };
  149. class Deserializer {
  150. public:
  151. Deserializer(JS::VM& vm, JS::Realm& target_realm, SerializationRecord const& v)
  152. : m_vm(vm)
  153. , m_vector(v)
  154. , m_memory(target_realm.heap())
  155. {
  156. }
  157. WebIDL::ExceptionOr<void> deserialize()
  158. {
  159. // First pass: fill up the memory with new values
  160. u32 position = 0;
  161. while (position < m_vector.size()) {
  162. switch (m_vector[position++]) {
  163. case ValueTag::UndefinedPrimitive: {
  164. m_memory.append(JS::js_undefined());
  165. break;
  166. }
  167. case ValueTag::NullPrimitive: {
  168. m_memory.append(JS::js_null());
  169. break;
  170. }
  171. case ValueTag::BooleanPrimitive: {
  172. m_memory.append(JS::Value(static_cast<bool>(m_vector[position++])));
  173. break;
  174. }
  175. case ValueTag::NumberPrimitive: {
  176. u32 bits[2];
  177. bits[0] = m_vector[position++];
  178. bits[1] = m_vector[position++];
  179. double value = *bit_cast<double*>(&bits);
  180. m_memory.append(JS::Value(value));
  181. break;
  182. }
  183. case ValueTag::BigIntPrimitive: {
  184. auto big_int = TRY(deserialize_big_int_primitive(m_vm, m_vector, position));
  185. m_memory.append(JS::Value { big_int });
  186. break;
  187. }
  188. case ValueTag::StringPrimitive: {
  189. auto string = TRY(deserialize_string_primitive(m_vm, m_vector, position));
  190. m_memory.append(JS::Value { string });
  191. break;
  192. }
  193. case BooleanObject: {
  194. auto* realm = m_vm.current_realm();
  195. bool const value = static_cast<bool>(m_vector[position++]);
  196. m_memory.append(JS::BooleanObject::create(*realm, value));
  197. break;
  198. }
  199. case ValueTag::NumberObject: {
  200. auto* realm = m_vm.current_realm();
  201. u32 bits[2];
  202. bits[0] = m_vector[position++];
  203. bits[1] = m_vector[position++];
  204. double const value = *bit_cast<double*>(&bits);
  205. m_memory.append(JS::NumberObject::create(*realm, value));
  206. break;
  207. }
  208. case ValueTag::StringObject: {
  209. auto* realm = m_vm.current_realm();
  210. auto string = TRY(deserialize_string_primitive(m_vm, m_vector, position));
  211. m_memory.append(TRY(JS::StringObject::create(*realm, string, realm->intrinsics().string_prototype())));
  212. break;
  213. }
  214. case ValueTag::DateObject: {
  215. auto* realm = m_vm.current_realm();
  216. u32 bits[2];
  217. bits[0] = m_vector[position++];
  218. bits[1] = m_vector[position++];
  219. double const value = *bit_cast<double*>(&bits);
  220. m_memory.append(JS::Date::create(*realm, value));
  221. break;
  222. }
  223. default:
  224. m_error = "Unsupported type"sv;
  225. break;
  226. }
  227. }
  228. return {};
  229. }
  230. WebIDL::ExceptionOr<JS::Value> result()
  231. {
  232. if (m_error.is_null())
  233. return m_memory[0];
  234. return throw_completion(WebIDL::DataCloneError::create(*m_vm.current_realm(), m_error));
  235. }
  236. private:
  237. JS::VM& m_vm;
  238. SerializationRecord const& m_vector;
  239. JS::MarkedVector<JS::Value> m_memory; // Index -> JS value
  240. StringView m_error;
  241. static WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::PrimitiveString>> deserialize_string_primitive(JS::VM& vm, Vector<u32> const& vector, u32& position)
  242. {
  243. u32 size_bits[2];
  244. size_bits[0] = vector[position++];
  245. size_bits[1] = vector[position++];
  246. u64 const size = *bit_cast<u64*>(&size_bits);
  247. Vector<u8> bytes;
  248. TRY_OR_THROW_OOM(vm, bytes.try_ensure_capacity(size));
  249. u64 byte_position = 0;
  250. while (position < vector.size()) {
  251. for (u8 i = 0; i < 4; ++i) {
  252. bytes.append(vector[position] >> (i * 8) & 0xFF);
  253. byte_position++;
  254. if (byte_position == size)
  255. break;
  256. }
  257. position++;
  258. }
  259. return TRY(Bindings::throw_dom_exception_if_needed(vm, [&vm, &bytes]() {
  260. return JS::PrimitiveString::create(vm, StringView { bytes });
  261. }));
  262. }
  263. static WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::BigInt>> deserialize_big_int_primitive(JS::VM& vm, Vector<u32> const& vector, u32& position)
  264. {
  265. auto string = TRY(deserialize_string_primitive(vm, vector, position));
  266. auto string_view = TRY(Bindings::throw_dom_exception_if_needed(vm, [&string]() {
  267. return string->utf8_string_view();
  268. }));
  269. return JS::BigInt::create(vm, ::Crypto::SignedBigInteger::from_base(10, string_view.substring_view(0, string_view.length() - 1)));
  270. }
  271. };
  272. // https://html.spec.whatwg.org/multipage/structured-data.html#structuredserialize
  273. WebIDL::ExceptionOr<SerializationRecord> structured_serialize(JS::VM& vm, JS::Value value)
  274. {
  275. // 1. Return ? StructuredSerializeInternal(value, false).
  276. return structured_serialize_internal(vm, value, false, {});
  277. }
  278. // https://html.spec.whatwg.org/multipage/structured-data.html#structuredserializeforstorage
  279. WebIDL::ExceptionOr<SerializationRecord> structured_serialize_for_storage(JS::VM& vm, JS::Value value)
  280. {
  281. // 1. Return ? StructuredSerializeInternal(value, true).
  282. return structured_serialize_internal(vm, value, true, {});
  283. }
  284. // https://html.spec.whatwg.org/multipage/structured-data.html#structuredserializeinternal
  285. WebIDL::ExceptionOr<SerializationRecord> structured_serialize_internal(JS::VM& vm, JS::Value value, bool for_storage, Optional<SerializationMemory> memory)
  286. {
  287. // FIXME: Do the spec steps
  288. (void)for_storage;
  289. (void)memory;
  290. Serializer serializer(vm);
  291. TRY(serializer.serialize(value));
  292. return serializer.result(); // TODO: Avoid several copies of vector
  293. }
  294. // https://html.spec.whatwg.org/multipage/structured-data.html#structureddeserialize
  295. WebIDL::ExceptionOr<JS::Value> structured_deserialize(JS::VM& vm, SerializationRecord const& serialized, JS::Realm& target_realm, Optional<SerializationMemory> memory)
  296. {
  297. // FIXME: Do the spec steps
  298. (void)memory;
  299. Deserializer deserializer(vm, target_realm, serialized);
  300. TRY(deserializer.deserialize());
  301. return deserializer.result();
  302. }
  303. }