Commit 39eacf64 authored by Yang Guo's avatar Yang Guo Committed by Commit Bot

[snapshot] consistently use uint32_t and little endian encoding.

Bug: v8:6747
Change-Id: If4b9c1ccd23c3ef1ed5e173f0143d2cc85581154
Reviewed-on: https://chromium-review.googlesource.com/631678Reviewed-by: 's avatarJakob Gruber <jgruber@chromium.org>
Commit-Queue: Yang Guo <yangguo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#47567}
parent 237f0f32
...@@ -342,12 +342,14 @@ SerializedCodeData::SerializedCodeData(const std::vector<byte>* payload, ...@@ -342,12 +342,14 @@ SerializedCodeData::SerializedCodeData(const std::vector<byte>* payload,
cs->EncodeReservations(&reservations); cs->EncodeReservations(&reservations);
// Calculate sizes. // Calculate sizes.
int reservation_size = static_cast<int>(reservations.size()) * kInt32Size; uint32_t reservation_size =
int num_stub_keys = static_cast<int>(stub_keys->size()); static_cast<uint32_t>(reservations.size()) * kUInt32Size;
int stub_keys_size = num_stub_keys * kInt32Size; uint32_t num_stub_keys = static_cast<uint32_t>(stub_keys->size());
int payload_offset = kHeaderSize + reservation_size + stub_keys_size; uint32_t stub_keys_size = num_stub_keys * kUInt32Size;
int padded_payload_offset = POINTER_SIZE_ALIGN(payload_offset); uint32_t payload_offset = kHeaderSize + reservation_size + stub_keys_size;
int size = padded_payload_offset + static_cast<int>(payload->size()); uint32_t padded_payload_offset = POINTER_SIZE_ALIGN(payload_offset);
uint32_t size =
padded_payload_offset + static_cast<uint32_t>(payload->size());
// Allocate backing store and create result data. // Allocate backing store and create result data.
AllocateData(size); AllocateData(size);
...@@ -359,9 +361,10 @@ SerializedCodeData::SerializedCodeData(const std::vector<byte>* payload, ...@@ -359,9 +361,10 @@ SerializedCodeData::SerializedCodeData(const std::vector<byte>* payload,
SetHeaderValue(kCpuFeaturesOffset, SetHeaderValue(kCpuFeaturesOffset,
static_cast<uint32_t>(CpuFeatures::SupportedFeatures())); static_cast<uint32_t>(CpuFeatures::SupportedFeatures()));
SetHeaderValue(kFlagHashOffset, FlagList::Hash()); SetHeaderValue(kFlagHashOffset, FlagList::Hash());
SetHeaderValue(kNumReservationsOffset, static_cast<int>(reservations.size())); SetHeaderValue(kNumReservationsOffset,
static_cast<uint32_t>(reservations.size()));
SetHeaderValue(kNumCodeStubKeysOffset, num_stub_keys); SetHeaderValue(kNumCodeStubKeysOffset, num_stub_keys);
SetHeaderValue(kPayloadLengthOffset, static_cast<int>(payload->size())); SetHeaderValue(kPayloadLengthOffset, static_cast<uint32_t>(payload->size()));
// Zero out any padding in the header. // Zero out any padding in the header.
memset(data_ + kUnalignedHeaderSize, 0, kHeaderSize - kUnalignedHeaderSize); memset(data_ + kUnalignedHeaderSize, 0, kHeaderSize - kUnalignedHeaderSize);
......
...@@ -104,16 +104,18 @@ class SerializedCodeData : public SerializedData { ...@@ -104,16 +104,18 @@ class SerializedCodeData : public SerializedData {
// ... reservations // ... reservations
// ... code stub keys // ... code stub keys
// ... serialized payload // ... serialized payload
static const int kSourceHashOffset = kVersionHashOffset + kInt32Size; static const uint32_t kSourceHashOffset = kVersionHashOffset + kUInt32Size;
static const int kCpuFeaturesOffset = kSourceHashOffset + kInt32Size; static const uint32_t kCpuFeaturesOffset = kSourceHashOffset + kUInt32Size;
static const int kFlagHashOffset = kCpuFeaturesOffset + kInt32Size; static const uint32_t kFlagHashOffset = kCpuFeaturesOffset + kUInt32Size;
static const int kNumReservationsOffset = kFlagHashOffset + kInt32Size; static const uint32_t kNumReservationsOffset = kFlagHashOffset + kUInt32Size;
static const int kNumCodeStubKeysOffset = kNumReservationsOffset + kInt32Size; static const uint32_t kNumCodeStubKeysOffset =
static const int kPayloadLengthOffset = kNumCodeStubKeysOffset + kInt32Size; kNumReservationsOffset + kUInt32Size;
static const int kChecksum1Offset = kPayloadLengthOffset + kInt32Size; static const uint32_t kPayloadLengthOffset =
static const int kChecksum2Offset = kChecksum1Offset + kInt32Size; kNumCodeStubKeysOffset + kUInt32Size;
static const int kUnalignedHeaderSize = kChecksum2Offset + kInt32Size; static const uint32_t kChecksum1Offset = kPayloadLengthOffset + kUInt32Size;
static const int kHeaderSize = POINTER_SIZE_ALIGN(kUnalignedHeaderSize); static const uint32_t kChecksum2Offset = kChecksum1Offset + kUInt32Size;
static const uint32_t kUnalignedHeaderSize = kChecksum2Offset + kUInt32Size;
static const uint32_t kHeaderSize = POINTER_SIZE_ALIGN(kUnalignedHeaderSize);
// Used when consuming. // Used when consuming.
static SerializedCodeData FromCachedData(Isolate* isolate, static SerializedCodeData FromCachedData(Isolate* isolate,
......
...@@ -57,7 +57,7 @@ const char* ExternalReferenceEncoder::NameOfAddress(Isolate* isolate, ...@@ -57,7 +57,7 @@ const char* ExternalReferenceEncoder::NameOfAddress(Isolate* isolate,
maybe_index.FromJust()); maybe_index.FromJust());
} }
void SerializedData::AllocateData(int size) { void SerializedData::AllocateData(uint32_t size) {
DCHECK(!owns_data_); DCHECK(!owns_data_);
data_ = NewArray<byte>(size); data_ = NewArray<byte>(size);
size_ = size; size_ = size;
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "src/base/bits.h" #include "src/base/bits.h"
#include "src/external-reference-table.h" #include "src/external-reference-table.h"
#include "src/globals.h" #include "src/globals.h"
#include "src/utils.h"
#include "src/visitors.h" #include "src/visitors.h"
namespace v8 { namespace v8 {
...@@ -268,24 +269,22 @@ class SerializedData { ...@@ -268,24 +269,22 @@ class SerializedData {
return table->num_api_references(); return table->num_api_references();
} }
static const int kMagicNumberOffset = 0; static const uint32_t kMagicNumberOffset = 0;
static const int kExtraExternalReferencesOffset = static const uint32_t kExtraExternalReferencesOffset =
kMagicNumberOffset + kInt32Size; kMagicNumberOffset + kUInt32Size;
static const int kVersionHashOffset = static const uint32_t kVersionHashOffset =
kExtraExternalReferencesOffset + kInt32Size; kExtraExternalReferencesOffset + kUInt32Size;
protected: protected:
void SetHeaderValue(int offset, uint32_t value) { void SetHeaderValue(uint32_t offset, uint32_t value) {
memcpy(data_ + offset, &value, sizeof(value)); WriteLittleEndianValue(data_ + offset, value);
} }
uint32_t GetHeaderValue(int offset) const { uint32_t GetHeaderValue(uint32_t offset) const {
uint32_t value; return ReadLittleEndianValue<uint32_t>(data_ + offset);
memcpy(&value, data_ + offset, sizeof(value));
return value;
} }
void AllocateData(int size); void AllocateData(uint32_t size);
static uint32_t ComputeMagicNumber(Isolate* isolate) { static uint32_t ComputeMagicNumber(Isolate* isolate) {
return ComputeMagicNumber(ExternalReferenceTable::instance(isolate)); return ComputeMagicNumber(ExternalReferenceTable::instance(isolate));
...@@ -300,7 +299,7 @@ class SerializedData { ...@@ -300,7 +299,7 @@ class SerializedData {
} }
byte* data_; byte* data_;
int size_; uint32_t size_;
bool owns_data_; bool owns_data_;
private: private:
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "src/snapshot/partial-deserializer.h" #include "src/snapshot/partial-deserializer.h"
#include "src/snapshot/snapshot-source-sink.h" #include "src/snapshot/snapshot-source-sink.h"
#include "src/snapshot/startup-deserializer.h" #include "src/snapshot/startup-deserializer.h"
#include "src/utils.h"
#include "src/version.h" #include "src/version.h"
namespace v8 { namespace v8 {
...@@ -61,7 +62,7 @@ MaybeHandle<Context> Snapshot::NewContextFromSnapshot( ...@@ -61,7 +62,7 @@ MaybeHandle<Context> Snapshot::NewContextFromSnapshot(
const v8::StartupData* blob = isolate->snapshot_blob(); const v8::StartupData* blob = isolate->snapshot_blob();
bool can_rehash = ExtractRehashability(blob); bool can_rehash = ExtractRehashability(blob);
Vector<const byte> context_data = Vector<const byte> context_data =
ExtractContextData(blob, static_cast<int>(context_index)); ExtractContextData(blob, static_cast<uint32_t>(context_index));
SnapshotData snapshot_data(context_data); SnapshotData snapshot_data(context_data);
MaybeHandle<Context> maybe_result = PartialDeserializer::DeserializeContext( MaybeHandle<Context> maybe_result = PartialDeserializer::DeserializeContext(
...@@ -103,93 +104,98 @@ void ProfileDeserialization( ...@@ -103,93 +104,98 @@ void ProfileDeserialization(
v8::StartupData Snapshot::CreateSnapshotBlob( v8::StartupData Snapshot::CreateSnapshotBlob(
const SnapshotData* startup_snapshot, const SnapshotData* startup_snapshot,
const std::vector<SnapshotData*>& context_snapshots, bool can_be_rehashed) { const std::vector<SnapshotData*>& context_snapshots, bool can_be_rehashed) {
int num_contexts = static_cast<int>(context_snapshots.size()); uint32_t num_contexts = static_cast<uint32_t>(context_snapshots.size());
int startup_snapshot_offset = StartupSnapshotOffset(num_contexts); uint32_t startup_snapshot_offset = StartupSnapshotOffset(num_contexts);
int total_length = startup_snapshot_offset; uint32_t total_length = startup_snapshot_offset;
total_length += startup_snapshot->RawData().length(); total_length += static_cast<uint32_t>(startup_snapshot->RawData().length());
for (const auto context_snapshot : context_snapshots) { for (const auto context_snapshot : context_snapshots) {
total_length += context_snapshot->RawData().length(); total_length += static_cast<uint32_t>(context_snapshot->RawData().length());
} }
ProfileDeserialization(startup_snapshot, context_snapshots); ProfileDeserialization(startup_snapshot, context_snapshots);
char* data = new char[total_length]; char* data = new char[total_length];
memcpy(data + kNumberOfContextsOffset, &num_contexts, kInt32Size); SetHeaderValue(data, kNumberOfContextsOffset, num_contexts);
int rehashability = can_be_rehashed ? 1 : 0; SetHeaderValue(data, kRehashabilityOffset, can_be_rehashed ? 1 : 0);
memcpy(data + kRehashabilityOffset, &rehashability, kInt32Size); uint32_t payload_offset = StartupSnapshotOffset(num_contexts);
int payload_offset = StartupSnapshotOffset(num_contexts); uint32_t payload_length =
int payload_length = startup_snapshot->RawData().length(); static_cast<uint32_t>(startup_snapshot->RawData().length());
memcpy(data + payload_offset, startup_snapshot->RawData().start(), CopyBytes(data + payload_offset,
payload_length); reinterpret_cast<const char*>(startup_snapshot->RawData().start()),
payload_length);
if (FLAG_profile_deserialization) { if (FLAG_profile_deserialization) {
PrintF("Snapshot blob consists of:\n%10d bytes for startup\n", PrintF("Snapshot blob consists of:\n%10d bytes for startup\n",
payload_length); payload_length);
} }
payload_offset += payload_length; payload_offset += payload_length;
for (int i = 0; i < num_contexts; i++) { for (uint32_t i = 0; i < num_contexts; i++) {
memcpy(data + ContextSnapshotOffsetOffset(i), &payload_offset, kInt32Size); SetHeaderValue(data, ContextSnapshotOffsetOffset(i), payload_offset);
SnapshotData* context_snapshot = context_snapshots[i]; SnapshotData* context_snapshot = context_snapshots[i];
payload_length = context_snapshot->RawData().length(); payload_length = context_snapshot->RawData().length();
memcpy(data + payload_offset, context_snapshot->RawData().start(), CopyBytes(
payload_length); data + payload_offset,
reinterpret_cast<const char*>(context_snapshot->RawData().start()),
payload_length);
if (FLAG_profile_deserialization) { if (FLAG_profile_deserialization) {
PrintF("%10d bytes for context #%d\n", payload_length, i); PrintF("%10d bytes for context #%d\n", payload_length, i);
} }
payload_offset += payload_length; payload_offset += payload_length;
} }
v8::StartupData result = {data, total_length}; v8::StartupData result = {data, static_cast<int>(total_length)};
return result; return result;
} }
int Snapshot::ExtractNumContexts(const v8::StartupData* data) { uint32_t Snapshot::ExtractNumContexts(const v8::StartupData* data) {
CHECK_LT(kNumberOfContextsOffset, data->raw_size); CHECK_LT(kNumberOfContextsOffset, data->raw_size);
int num_contexts; uint32_t num_contexts = GetHeaderValue(data, kNumberOfContextsOffset);
memcpy(&num_contexts, data->data + kNumberOfContextsOffset, kInt32Size);
return num_contexts; return num_contexts;
} }
uint32_t Snapshot::ExtractContextOffset(const v8::StartupData* data,
uint32_t index) {
// Extract the offset of the context at a given index from the StartupData,
// and check that it is within bounds.
uint32_t context_offset =
GetHeaderValue(data, ContextSnapshotOffsetOffset(index));
CHECK_LT(context_offset, static_cast<uint32_t>(data->raw_size));
return context_offset;
}
bool Snapshot::ExtractRehashability(const v8::StartupData* data) { bool Snapshot::ExtractRehashability(const v8::StartupData* data) {
CHECK_LT(kRehashabilityOffset, data->raw_size); CHECK_LT(kRehashabilityOffset, static_cast<uint32_t>(data->raw_size));
int rehashability; return GetHeaderValue(data, kRehashabilityOffset) != 0;
memcpy(&rehashability, data->data + kRehashabilityOffset, kInt32Size);
return rehashability != 0;
} }
Vector<const byte> Snapshot::ExtractStartupData(const v8::StartupData* data) { Vector<const byte> Snapshot::ExtractStartupData(const v8::StartupData* data) {
int num_contexts = ExtractNumContexts(data); uint32_t num_contexts = ExtractNumContexts(data);
int startup_offset = StartupSnapshotOffset(num_contexts); uint32_t startup_offset = StartupSnapshotOffset(num_contexts);
CHECK_LT(startup_offset, data->raw_size); CHECK_LT(startup_offset, data->raw_size);
int first_context_offset; uint32_t first_context_offset = ExtractContextOffset(data, 0);
memcpy(&first_context_offset, data->data + ContextSnapshotOffsetOffset(0),
kInt32Size);
CHECK_LT(first_context_offset, data->raw_size); CHECK_LT(first_context_offset, data->raw_size);
int startup_length = first_context_offset - startup_offset; uint32_t startup_length = first_context_offset - startup_offset;
const byte* startup_data = const byte* startup_data =
reinterpret_cast<const byte*>(data->data + startup_offset); reinterpret_cast<const byte*>(data->data + startup_offset);
return Vector<const byte>(startup_data, startup_length); return Vector<const byte>(startup_data, startup_length);
} }
Vector<const byte> Snapshot::ExtractContextData(const v8::StartupData* data, Vector<const byte> Snapshot::ExtractContextData(const v8::StartupData* data,
int index) { uint32_t index) {
int num_contexts = ExtractNumContexts(data); uint32_t num_contexts = ExtractNumContexts(data);
CHECK_LT(index, num_contexts); CHECK_LT(index, num_contexts);
int context_offset; uint32_t context_offset = ExtractContextOffset(data, index);
memcpy(&context_offset, data->data + ContextSnapshotOffsetOffset(index), uint32_t next_context_offset;
kInt32Size);
int next_context_offset;
if (index == num_contexts - 1) { if (index == num_contexts - 1) {
next_context_offset = data->raw_size; next_context_offset = data->raw_size;
} else { } else {
memcpy(&next_context_offset, next_context_offset = ExtractContextOffset(data, index + 1);
data->data + ContextSnapshotOffsetOffset(index + 1), kInt32Size);
CHECK_LT(next_context_offset, data->raw_size); CHECK_LT(next_context_offset, data->raw_size);
} }
const byte* context_data = const byte* context_data =
reinterpret_cast<const byte*>(data->data + context_offset); reinterpret_cast<const byte*>(data->data + context_offset);
int context_length = next_context_offset - context_offset; uint32_t context_length = next_context_offset - context_offset;
return Vector<const byte>(context_data, context_length); return Vector<const byte>(context_data, context_length);
} }
...@@ -200,8 +206,10 @@ SnapshotData::SnapshotData(const Serializer* serializer) { ...@@ -200,8 +206,10 @@ SnapshotData::SnapshotData(const Serializer* serializer) {
const std::vector<byte>* payload = serializer->sink()->data(); const std::vector<byte>* payload = serializer->sink()->data();
// Calculate sizes. // Calculate sizes.
int reservation_size = static_cast<int>(reservations.size()) * kInt32Size; uint32_t reservation_size =
int size = kHeaderSize + reservation_size + static_cast<int>(payload->size()); static_cast<uint32_t>(reservations.size()) * kUInt32Size;
uint32_t size =
kHeaderSize + reservation_size + static_cast<uint32_t>(payload->size());
// Allocate backing store and create result data. // Allocate backing store and create result data.
AllocateData(size); AllocateData(size);
...@@ -232,9 +240,10 @@ Vector<const SerializedData::Reservation> SnapshotData::Reservations() const { ...@@ -232,9 +240,10 @@ Vector<const SerializedData::Reservation> SnapshotData::Reservations() const {
} }
Vector<const byte> SnapshotData::Payload() const { Vector<const byte> SnapshotData::Payload() const {
int reservations_size = GetHeaderValue(kNumReservationsOffset) * kInt32Size; uint32_t reservations_size =
GetHeaderValue(kNumReservationsOffset) * kUInt32Size;
const byte* payload = data_ + kHeaderSize + reservations_size; const byte* payload = data_ + kHeaderSize + reservations_size;
int length = GetHeaderValue(kPayloadLengthOffset); uint32_t length = GetHeaderValue(kPayloadLengthOffset);
DCHECK_EQ(data_ + size_, payload + length); DCHECK_EQ(data_ + size_, payload + length);
return Vector<const byte>(payload, length); return Vector<const byte>(payload, length);
} }
......
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
#include "src/snapshot/partial-serializer.h" #include "src/snapshot/partial-serializer.h"
#include "src/snapshot/startup-serializer.h" #include "src/snapshot/startup-serializer.h"
#include "src/utils.h"
namespace v8 { namespace v8 {
namespace internal { namespace internal {
...@@ -46,9 +48,11 @@ class SnapshotData : public SerializedData { ...@@ -46,9 +48,11 @@ class SnapshotData : public SerializedData {
// [4] payload length // [4] payload length
// ... reservations // ... reservations
// ... serialized payload // ... serialized payload
static const int kNumReservationsOffset = kVersionHashOffset + kInt32Size; static const uint32_t kNumReservationsOffset =
static const int kPayloadLengthOffset = kNumReservationsOffset + kInt32Size; kVersionHashOffset + kUInt32Size;
static const int kHeaderSize = kPayloadLengthOffset + kInt32Size; static const uint32_t kPayloadLengthOffset =
kNumReservationsOffset + kUInt32Size;
static const uint32_t kHeaderSize = kPayloadLengthOffset + kUInt32Size;
}; };
class Snapshot : public AllStatic { class Snapshot : public AllStatic {
...@@ -79,11 +83,20 @@ class Snapshot : public AllStatic { ...@@ -79,11 +83,20 @@ class Snapshot : public AllStatic {
#endif // DEBUG #endif // DEBUG
private: private:
static int ExtractNumContexts(const v8::StartupData* data); static uint32_t ExtractNumContexts(const v8::StartupData* data);
static uint32_t ExtractContextOffset(const v8::StartupData* data,
uint32_t index);
static bool ExtractRehashability(const v8::StartupData* data); static bool ExtractRehashability(const v8::StartupData* data);
static Vector<const byte> ExtractStartupData(const v8::StartupData* data); static Vector<const byte> ExtractStartupData(const v8::StartupData* data);
static Vector<const byte> ExtractContextData(const v8::StartupData* data, static Vector<const byte> ExtractContextData(const v8::StartupData* data,
int index); uint32_t index);
static uint32_t GetHeaderValue(const v8::StartupData* data, uint32_t offset) {
return ReadLittleEndianValue<uint32_t>(data->data + offset);
}
static void SetHeaderValue(char* data, uint32_t offset, uint32_t value) {
WriteLittleEndianValue(data + offset, value);
}
// Snapshot blob layout: // Snapshot blob layout:
// [0] number of contexts N // [0] number of contexts N
...@@ -96,17 +109,18 @@ class Snapshot : public AllStatic { ...@@ -96,17 +109,18 @@ class Snapshot : public AllStatic {
// ... context 0 snapshot data // ... context 0 snapshot data
// ... context 1 snapshot data // ... context 1 snapshot data
static const int kNumberOfContextsOffset = 0; static const uint32_t kNumberOfContextsOffset = 0;
// TODO(yangguo): generalize rehashing, and remove this flag. // TODO(yangguo): generalize rehashing, and remove this flag.
static const int kRehashabilityOffset = kNumberOfContextsOffset + kInt32Size; static const uint32_t kRehashabilityOffset =
static const int kFirstContextOffsetOffset = kNumberOfContextsOffset + kUInt32Size;
kRehashabilityOffset + kInt32Size; static const uint32_t kFirstContextOffsetOffset =
kRehashabilityOffset + kUInt32Size;
static int StartupSnapshotOffset(int num_contexts) { static uint32_t StartupSnapshotOffset(int num_contexts) {
return kFirstContextOffsetOffset + num_contexts * kInt32Size; return kFirstContextOffsetOffset + num_contexts * kInt32Size;
} }
static int ContextSnapshotOffsetOffset(int index) { static uint32_t ContextSnapshotOffsetOffset(int index) {
return kFirstContextOffsetOffset + index * kInt32Size; return kFirstContextOffsetOffset + index * kInt32Size;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment