| OLD | NEW |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/value-serializer.h" | 5 #include "src/value-serializer.h" |
| 6 | 6 |
| 7 #include <type_traits> | 7 #include <type_traits> |
| 8 | 8 |
| 9 #include "src/base/logging.h" | 9 #include "src/base/logging.h" |
| 10 #include "src/conversions.h" | 10 #include "src/conversions.h" |
| 11 #include "src/factory.h" | 11 #include "src/factory.h" |
| 12 #include "src/flags.h" | 12 #include "src/flags.h" |
| 13 #include "src/handles-inl.h" | 13 #include "src/handles-inl.h" |
| 14 #include "src/isolate.h" | 14 #include "src/isolate.h" |
| 15 #include "src/objects-inl.h" | 15 #include "src/objects-inl.h" |
| 16 #include "src/objects.h" | 16 #include "src/objects.h" |
| 17 #include "src/snapshot/code-serializer.h" | 17 #include "src/snapshot/code-serializer.h" |
| 18 #include "src/transitions.h" | 18 #include "src/transitions.h" |
| 19 #include "src/wasm/wasm-module.h" | 19 #include "src/wasm/wasm-module.h" |
| 20 #include "src/wasm/wasm-objects.h" | 20 #include "src/wasm/wasm-objects.h" |
| 21 #include "src/wasm/wasm-result.h" | 21 #include "src/wasm/wasm-result.h" |
| 22 | 22 |
| 23 namespace v8 { | 23 namespace v8 { |
| 24 namespace internal { | 24 namespace internal { |
| 25 | 25 |
| 26 // Version 9: (imported from Blink) | 26 // Version 9: (imported from Blink) |
| 27 // Version 10: one-byte (Latin-1) strings | 27 // Version 10: one-byte (Latin-1) strings |
| 28 static const uint32_t kLatestVersion = 10; | 28 // Version 11: properly separate undefined from the hole in arrays |
| 29 static const uint32_t kLatestVersion = 11; |
| 29 | 30 |
| 30 static const int kPretenureThreshold = 100 * KB; | 31 static const int kPretenureThreshold = 100 * KB; |
| 31 | 32 |
| 32 template <typename T> | 33 template <typename T> |
| 33 static size_t BytesNeededForVarint(T value) { | 34 static size_t BytesNeededForVarint(T value) { |
| 34 static_assert(std::is_integral<T>::value && std::is_unsigned<T>::value, | 35 static_assert(std::is_integral<T>::value && std::is_unsigned<T>::value, |
| 35 "Only unsigned integer types can be written as varints."); | 36 "Only unsigned integer types can be written as varints."); |
| 36 size_t result = 0; | 37 size_t result = 0; |
| 37 do { | 38 do { |
| 38 result++; | 39 result++; |
| 39 value >>= 7; | 40 value >>= 7; |
| 40 } while (value); | 41 } while (value); |
| 41 return result; | 42 return result; |
| 42 } | 43 } |
| 43 | 44 |
| 44 enum class SerializationTag : uint8_t { | 45 enum class SerializationTag : uint8_t { |
| 45 // version:uint32_t (if at beginning of data, sets version > 0) | 46 // version:uint32_t (if at beginning of data, sets version > 0) |
| 46 kVersion = 0xFF, | 47 kVersion = 0xFF, |
| 47 // ignore | 48 // ignore |
| 48 kPadding = '\0', | 49 kPadding = '\0', |
| 49 // refTableSize:uint32_t (previously used for sanity checks; safe to ignore) | 50 // refTableSize:uint32_t (previously used for sanity checks; safe to ignore) |
| 50 kVerifyObjectCount = '?', | 51 kVerifyObjectCount = '?', |
| 51 // Oddballs (no data). | 52 // Oddballs (no data). |
| 53 kTheHole = '-', |
| 52 kUndefined = '_', | 54 kUndefined = '_', |
| 53 kNull = '0', | 55 kNull = '0', |
| 54 kTrue = 'T', | 56 kTrue = 'T', |
| 55 kFalse = 'F', | 57 kFalse = 'F', |
| 56 // Number represented as 32-bit integer, ZigZag-encoded | 58 // Number represented as 32-bit integer, ZigZag-encoded |
| 57 // (like sint32 in protobuf) | 59 // (like sint32 in protobuf) |
| 58 kInt32 = 'I', | 60 kInt32 = 'I', |
| 59 // Number represented as 32-bit unsigned integer, varint-encoded | 61 // Number represented as 32-bit unsigned integer, varint-encoded |
| 60 // (like uint32 in protobuf) | 62 // (like uint32 in protobuf) |
| 61 kUint32 = 'U', | 63 kUint32 = 'U', |
| (...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 531 // To keep things simple, for now we decide between dense and sparse | 533 // To keep things simple, for now we decide between dense and sparse |
| 532 // serialization based on elements kind. A more principled heuristic could | 534 // serialization based on elements kind. A more principled heuristic could |
| 533 // count the elements, but would need to take care to note which indices | 535 // count the elements, but would need to take care to note which indices |
| 534 // existed (as only indices which were enumerable own properties at this point | 536 // existed (as only indices which were enumerable own properties at this point |
| 535 // should be serialized). | 537 // should be serialized). |
| 536 const bool should_serialize_densely = | 538 const bool should_serialize_densely = |
| 537 array->HasFastElements() && !array->HasFastHoleyElements(); | 539 array->HasFastElements() && !array->HasFastHoleyElements(); |
| 538 | 540 |
| 539 if (should_serialize_densely) { | 541 if (should_serialize_densely) { |
| 540 DCHECK_LE(length, static_cast<uint32_t>(FixedArray::kMaxLength)); | 542 DCHECK_LE(length, static_cast<uint32_t>(FixedArray::kMaxLength)); |
| 541 | |
| 542 // TODO(jbroman): Distinguish between undefined and a hole (this can happen | |
| 543 // if serializing one of the elements deletes another). This requires wire | |
| 544 // format changes. | |
| 545 WriteTag(SerializationTag::kBeginDenseJSArray); | 543 WriteTag(SerializationTag::kBeginDenseJSArray); |
| 546 WriteVarint<uint32_t>(length); | 544 WriteVarint<uint32_t>(length); |
| 547 uint32_t i = 0; | 545 uint32_t i = 0; |
| 548 | 546 |
| 549 // Fast paths. Note that FAST_ELEMENTS in particular can bail due to the | 547 // Fast paths. Note that FAST_ELEMENTS in particular can bail due to the |
| 550 // structure of the elements changing. | 548 // structure of the elements changing. |
| 551 switch (array->GetElementsKind()) { | 549 switch (array->GetElementsKind()) { |
| 552 case FAST_SMI_ELEMENTS: { | 550 case FAST_SMI_ELEMENTS: { |
| 553 Handle<FixedArray> elements(FixedArray::cast(array->elements()), | 551 Handle<FixedArray> elements(FixedArray::cast(array->elements()), |
| 554 isolate_); | 552 isolate_); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 582 break; | 580 break; |
| 583 } | 581 } |
| 584 | 582 |
| 585 // If there are elements remaining, serialize them slowly. | 583 // If there are elements remaining, serialize them slowly. |
| 586 for (; i < length; i++) { | 584 for (; i < length; i++) { |
| 587 // Serializing the array's elements can have arbitrary side effects, so we | 585 // Serializing the array's elements can have arbitrary side effects, so we |
| 588 // cannot rely on still having fast elements, even if it did to begin | 586 // cannot rely on still having fast elements, even if it did to begin |
| 589 // with. | 587 // with. |
| 590 Handle<Object> element; | 588 Handle<Object> element; |
| 591 LookupIterator it(isolate_, array, i, array, LookupIterator::OWN); | 589 LookupIterator it(isolate_, array, i, array, LookupIterator::OWN); |
| 590 if (!it.IsFound()) { |
| 591 // This can happen in the case where an array that was originally dense |
| 592 // became sparse during serialization. It's too late to switch to the |
| 593 // sparse format, but we can mark the elements as absent. |
| 594 WriteTag(SerializationTag::kTheHole); |
| 595 continue; |
| 596 } |
| 592 if (!Object::GetProperty(&it).ToHandle(&element) || | 597 if (!Object::GetProperty(&it).ToHandle(&element) || |
| 593 !WriteObject(element).FromMaybe(false)) { | 598 !WriteObject(element).FromMaybe(false)) { |
| 594 return Nothing<bool>(); | 599 return Nothing<bool>(); |
| 595 } | 600 } |
| 596 } | 601 } |
| 597 | 602 |
| 598 KeyAccumulator accumulator(isolate_, KeyCollectionMode::kOwnOnly, | 603 KeyAccumulator accumulator(isolate_, KeyCollectionMode::kOwnOnly, |
| 599 ENUMERABLE_STRINGS); | 604 ENUMERABLE_STRINGS); |
| 600 if (!accumulator.CollectOwnPropertyNames(array, array).FromMaybe(false)) { | 605 if (!accumulator.CollectOwnPropertyNames(array, array).FromMaybe(false)) { |
| 601 return Nothing<bool>(); | 606 return Nothing<bool>(); |
| (...skipping 711 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1313 | 1318 |
| 1314 uint32_t id = next_id_++; | 1319 uint32_t id = next_id_++; |
| 1315 HandleScope scope(isolate_); | 1320 HandleScope scope(isolate_); |
| 1316 Handle<JSArray> array = isolate_->factory()->NewJSArray( | 1321 Handle<JSArray> array = isolate_->factory()->NewJSArray( |
| 1317 FAST_HOLEY_ELEMENTS, length, length, INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE, | 1322 FAST_HOLEY_ELEMENTS, length, length, INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE, |
| 1318 pretenure_); | 1323 pretenure_); |
| 1319 AddObjectWithID(id, array); | 1324 AddObjectWithID(id, array); |
| 1320 | 1325 |
| 1321 Handle<FixedArray> elements(FixedArray::cast(array->elements()), isolate_); | 1326 Handle<FixedArray> elements(FixedArray::cast(array->elements()), isolate_); |
| 1322 for (uint32_t i = 0; i < length; i++) { | 1327 for (uint32_t i = 0; i < length; i++) { |
| 1328 SerializationTag tag; |
| 1329 if (PeekTag().To(&tag) && tag == SerializationTag::kTheHole) { |
| 1330 ConsumeTag(SerializationTag::kTheHole); |
| 1331 continue; |
| 1332 } |
| 1333 |
| 1323 Handle<Object> element; | 1334 Handle<Object> element; |
| 1324 if (!ReadObject().ToHandle(&element)) return MaybeHandle<JSArray>(); | 1335 if (!ReadObject().ToHandle(&element)) return MaybeHandle<JSArray>(); |
| 1325 // TODO(jbroman): Distinguish between undefined and a hole. | 1336 |
| 1326 if (element->IsUndefined(isolate_)) continue; | 1337 // Serialization versions less than 11 encode the hole the same as |
| 1338 // undefined. For consistency with previous behavior, store these as the |
| 1339 // hole. Past version 11, undefined means undefined. |
| 1340 if (version_ < 11 && element->IsUndefined(isolate_)) continue; |
| 1341 |
| 1327 elements->set(i, *element); | 1342 elements->set(i, *element); |
| 1328 } | 1343 } |
| 1329 | 1344 |
| 1330 uint32_t num_properties; | 1345 uint32_t num_properties; |
| 1331 uint32_t expected_num_properties; | 1346 uint32_t expected_num_properties; |
| 1332 uint32_t expected_length; | 1347 uint32_t expected_length; |
| 1333 if (!ReadJSObjectProperties(array, SerializationTag::kEndDenseJSArray, false) | 1348 if (!ReadJSObjectProperties(array, SerializationTag::kEndDenseJSArray, false) |
| 1334 .To(&num_properties) || | 1349 .To(&num_properties) || |
| 1335 !ReadVarint<uint32_t>().To(&expected_num_properties) || | 1350 !ReadVarint<uint32_t>().To(&expected_num_properties) || |
| 1336 !ReadVarint<uint32_t>().To(&expected_length) || | 1351 !ReadVarint<uint32_t>().To(&expected_length) || |
| (...skipping 582 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1919 if (stack.size() != 1) { | 1934 if (stack.size() != 1) { |
| 1920 isolate_->Throw(*isolate_->factory()->NewError( | 1935 isolate_->Throw(*isolate_->factory()->NewError( |
| 1921 MessageTemplate::kDataCloneDeserializationError)); | 1936 MessageTemplate::kDataCloneDeserializationError)); |
| 1922 return MaybeHandle<Object>(); | 1937 return MaybeHandle<Object>(); |
| 1923 } | 1938 } |
| 1924 return scope.CloseAndEscape(stack[0]); | 1939 return scope.CloseAndEscape(stack[0]); |
| 1925 } | 1940 } |
| 1926 | 1941 |
| 1927 } // namespace internal | 1942 } // namespace internal |
| 1928 } // namespace v8 | 1943 } // namespace v8 |
| OLD | NEW |