diff --git a/src/serialize.cc b/src/serialize.cc index e2ff9f42f3..f2c7cab3ce 100644 --- a/src/serialize.cc +++ b/src/serialize.cc @@ -2475,7 +2475,7 @@ SnapshotData::SnapshotData(const SnapshotByteSink& sink, // Set header values. SetHeaderValue(kCheckSumOffset, Version::Hash()); - SetHeaderValue(kReservationsOffset, reservations.length()); + SetHeaderValue(kNumReservationsOffset, reservations.length()); SetHeaderValue(kPayloadLengthOffset, payload.length()); // Copy reservation chunk sizes. @@ -2496,12 +2496,12 @@ bool SnapshotData::IsSane() { Vector SnapshotData::Reservations() const { return Vector( reinterpret_cast(data_ + kHeaderSize), - GetHeaderValue(kReservationsOffset)); + GetHeaderValue(kNumReservationsOffset)); } Vector SnapshotData::Payload() const { - int reservations_size = GetHeaderValue(kReservationsOffset) * kInt32Size; + int reservations_size = GetHeaderValue(kNumReservationsOffset) * kInt32Size; const byte* payload = data_ + kHeaderSize + reservations_size; int length = GetHeaderValue(kPayloadLengthOffset); DCHECK_EQ(data_ + size_, payload + length); @@ -2556,7 +2556,9 @@ SerializedCodeData::SerializedCodeData(const List& payload, int reservation_size = reservations.length() * kInt32Size; int num_stub_keys = stub_keys->length(); int stub_keys_size = stub_keys->length() * kInt32Size; - int size = kHeaderSize + reservation_size + stub_keys_size + payload.length(); + int payload_offset = kHeaderSize + reservation_size + stub_keys_size; + int padded_payload_offset = POINTER_SIZE_ALIGN(payload_offset); + int size = padded_payload_offset + payload.length(); // Allocate backing store and create result data. AllocateData(size); @@ -2568,7 +2570,7 @@ SerializedCodeData::SerializedCodeData(const List& payload, static_cast(CpuFeatures::SupportedFeatures())); SetHeaderValue(kFlagHashOffset, FlagList::Hash()); SetHeaderValue(kNumInternalizedStringsOffset, cs.num_internalized_strings()); - SetHeaderValue(kReservationsOffset, reservations.length()); + SetHeaderValue(kNumReservationsOffset, reservations.length()); SetHeaderValue(kNumCodeStubKeysOffset, num_stub_keys); SetHeaderValue(kPayloadLengthOffset, payload.length()); @@ -2584,9 +2586,11 @@ SerializedCodeData::SerializedCodeData(const List& payload, CopyBytes(data_ + kHeaderSize + reservation_size, reinterpret_cast(stub_keys->begin()), stub_keys_size); + memset(data_ + payload_offset, 0, padded_payload_offset - payload_offset); + // Copy serialized data. - CopyBytes(data_ + kHeaderSize + reservation_size + stub_keys_size, - payload.begin(), static_cast(payload.length())); + CopyBytes(data_ + padded_payload_offset, payload.begin(), + static_cast(payload.length())); } @@ -2616,15 +2620,17 @@ Vector SerializedCodeData::Reservations() const { return Vector( reinterpret_cast(data_ + kHeaderSize), - GetHeaderValue(kReservationsOffset)); + GetHeaderValue(kNumReservationsOffset)); } Vector SerializedCodeData::Payload() const { - int reservations_size = GetHeaderValue(kReservationsOffset) * kInt32Size; + int reservations_size = GetHeaderValue(kNumReservationsOffset) * kInt32Size; int code_stubs_size = GetHeaderValue(kNumCodeStubKeysOffset) * kInt32Size; - const byte* payload = - data_ + kHeaderSize + reservations_size + code_stubs_size; + int payload_offset = kHeaderSize + reservations_size + code_stubs_size; + int padded_payload_offset = POINTER_SIZE_ALIGN(payload_offset); + const byte* payload = data_ + padded_payload_offset; + DCHECK(IsAligned(reinterpret_cast(payload), kPointerAlignment)); int length = GetHeaderValue(kPayloadLengthOffset); DCHECK_EQ(data_ + size_, payload + length); return Vector(payload, length); @@ -2636,7 +2642,7 @@ int SerializedCodeData::NumInternalizedStrings() const { } Vector SerializedCodeData::CodeStubKeys() const { - int reservations_size = GetHeaderValue(kReservationsOffset) * kInt32Size; + int reservations_size = GetHeaderValue(kNumReservationsOffset) * kInt32Size; const byte* start = data_ + kHeaderSize + reservations_size; return Vector(reinterpret_cast(start), GetHeaderValue(kNumCodeStubKeysOffset)); diff --git a/src/serialize.h b/src/serialize.h index 264bd46f46..2b7f2ade9d 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -501,12 +501,13 @@ class SerializedData { protected: void SetHeaderValue(int offset, uint32_t value) { - memcpy(reinterpret_cast(data_) + offset, &value, sizeof(value)); + uint32_t* address = reinterpret_cast(data_ + offset); + memcpy(reinterpret_cast(address), &value, sizeof(value)); } uint32_t GetHeaderValue(int offset) const { uint32_t value; - memcpy(&value, reinterpret_cast(data_) + offset, sizeof(value)); + memcpy(&value, reinterpret_cast(data_ + offset), sizeof(value)); return value; } @@ -912,14 +913,16 @@ class SnapshotData : public SerializedData { private: bool IsSane(); - // The data header consists of int-sized entries: + // The data header consists of uint32_t-sized entries: // [0] version hash // [1] number of reservation size entries // [2] payload length + // ... reservations + // ... serialized payload static const int kCheckSumOffset = 0; - static const int kReservationsOffset = 1; - static const int kPayloadLengthOffset = 2; - static const int kHeaderSize = (kPayloadLengthOffset + 1) * kIntSize; + static const int kNumReservationsOffset = kCheckSumOffset + kInt32Size; + static const int kPayloadLengthOffset = kNumReservationsOffset + kInt32Size; + static const int kHeaderSize = kPayloadLengthOffset + kInt32Size; }; @@ -957,7 +960,7 @@ class SerializedCodeData : public SerializedData { uint32_t SourceHash(String* source) const { return source->length(); } - // The data header consists of int-sized entries: + // The data header consists of uint32_t-sized entries: // [0] version hash // [1] source hash // [2] cpu features @@ -966,18 +969,23 @@ class SerializedCodeData : public SerializedData { // [5] number of code stub keys // [6] number of reservation size entries // [7] payload length + // [8] payload checksum part 1 + // [9] payload checksum part 2 + // ... reservations + // ... code stub keys + // ... serialized payload static const int kVersionHashOffset = 0; - static const int kSourceHashOffset = 1; - static const int kCpuFeaturesOffset = 2; - static const int kFlagHashOffset = 3; - static const int kNumInternalizedStringsOffset = 4; - static const int kReservationsOffset = 5; - static const int kNumCodeStubKeysOffset = 6; - static const int kPayloadLengthOffset = 7; - static const int kChecksum1Offset = 8; - static const int kChecksum2Offset = 9; - static const int kHeaderSize = - POINTER_SIZE_ALIGN((kChecksum2Offset + 1) * kIntSize); + static const int kSourceHashOffset = kVersionHashOffset + kInt32Size; + static const int kCpuFeaturesOffset = kSourceHashOffset + kInt32Size; + static const int kFlagHashOffset = kCpuFeaturesOffset + kInt32Size; + static const int kNumInternalizedStringsOffset = kFlagHashOffset + kInt32Size; + static const int kNumReservationsOffset = + kNumInternalizedStringsOffset + kInt32Size; + static const int kNumCodeStubKeysOffset = kNumReservationsOffset + kInt32Size; + static const int kPayloadLengthOffset = kNumCodeStubKeysOffset + kInt32Size; + static const int kChecksum1Offset = kPayloadLengthOffset + kInt32Size; + static const int kChecksum2Offset = kChecksum1Offset + kInt32Size; + static const int kHeaderSize = kChecksum2Offset + kInt32Size; }; } } // namespace v8::internal