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,
cs->EncodeReservations(&reservations);
// Calculate sizes.
int reservation_size = static_cast<int>(reservations.size()) * kInt32Size;
int num_stub_keys = static_cast<int>(stub_keys->size());
int stub_keys_size = num_stub_keys * kInt32Size;
int payload_offset = kHeaderSize + reservation_size + stub_keys_size;
int padded_payload_offset = POINTER_SIZE_ALIGN(payload_offset);
int size = padded_payload_offset + static_cast<int>(payload->size());
uint32_t reservation_size =
static_cast<uint32_t>(reservations.size()) * kUInt32Size;
uint32_t num_stub_keys = static_cast<uint32_t>(stub_keys->size());
uint32_t stub_keys_size = num_stub_keys * kUInt32Size;
uint32_t payload_offset = kHeaderSize + reservation_size + stub_keys_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.
AllocateData(size);
......@@ -359,9 +361,10 @@ SerializedCodeData::SerializedCodeData(const std::vector<byte>* payload,
SetHeaderValue(kCpuFeaturesOffset,
static_cast<uint32_t>(CpuFeatures::SupportedFeatures()));
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(kPayloadLengthOffset, static_cast<int>(payload->size()));
SetHeaderValue(kPayloadLengthOffset, static_cast<uint32_t>(payload->size()));
// Zero out any padding in the header.
memset(data_ + kUnalignedHeaderSize, 0, kHeaderSize - kUnalignedHeaderSize);
......
......@@ -104,16 +104,18 @@ class SerializedCodeData : public SerializedData {
// ... reservations
// ... code stub keys
// ... serialized payload
static const int kSourceHashOffset = kVersionHashOffset + kInt32Size;
static const int kCpuFeaturesOffset = kSourceHashOffset + kInt32Size;
static const int kFlagHashOffset = kCpuFeaturesOffset + kInt32Size;
static const int kNumReservationsOffset = kFlagHashOffset + 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 kUnalignedHeaderSize = kChecksum2Offset + kInt32Size;
static const int kHeaderSize = POINTER_SIZE_ALIGN(kUnalignedHeaderSize);
static const uint32_t kSourceHashOffset = kVersionHashOffset + kUInt32Size;
static const uint32_t kCpuFeaturesOffset = kSourceHashOffset + kUInt32Size;
static const uint32_t kFlagHashOffset = kCpuFeaturesOffset + kUInt32Size;
static const uint32_t kNumReservationsOffset = kFlagHashOffset + kUInt32Size;
static const uint32_t kNumCodeStubKeysOffset =
kNumReservationsOffset + kUInt32Size;
static const uint32_t kPayloadLengthOffset =
kNumCodeStubKeysOffset + kUInt32Size;
static const uint32_t kChecksum1Offset = kPayloadLengthOffset + kUInt32Size;
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.
static SerializedCodeData FromCachedData(Isolate* isolate,
......
......@@ -57,7 +57,7 @@ const char* ExternalReferenceEncoder::NameOfAddress(Isolate* isolate,
maybe_index.FromJust());
}
void SerializedData::AllocateData(int size) {
void SerializedData::AllocateData(uint32_t size) {
DCHECK(!owns_data_);
data_ = NewArray<byte>(size);
size_ = size;
......
......@@ -9,6 +9,7 @@
#include "src/base/bits.h"
#include "src/external-reference-table.h"
#include "src/globals.h"
#include "src/utils.h"
#include "src/visitors.h"
namespace v8 {
......@@ -268,24 +269,22 @@ class SerializedData {
return table->num_api_references();
}
static const int kMagicNumberOffset = 0;
static const int kExtraExternalReferencesOffset =
kMagicNumberOffset + kInt32Size;
static const int kVersionHashOffset =
kExtraExternalReferencesOffset + kInt32Size;
static const uint32_t kMagicNumberOffset = 0;
static const uint32_t kExtraExternalReferencesOffset =
kMagicNumberOffset + kUInt32Size;
static const uint32_t kVersionHashOffset =
kExtraExternalReferencesOffset + kUInt32Size;
protected:
void SetHeaderValue(int offset, uint32_t value) {
memcpy(data_ + offset, &value, sizeof(value));
void SetHeaderValue(uint32_t offset, uint32_t value) {
WriteLittleEndianValue(data_ + offset, value);
}
uint32_t GetHeaderValue(int offset) const {
uint32_t value;
memcpy(&value, data_ + offset, sizeof(value));
return value;
uint32_t GetHeaderValue(uint32_t offset) const {
return ReadLittleEndianValue<uint32_t>(data_ + offset);
}
void AllocateData(int size);
void AllocateData(uint32_t size);
static uint32_t ComputeMagicNumber(Isolate* isolate) {
return ComputeMagicNumber(ExternalReferenceTable::instance(isolate));
......@@ -300,7 +299,7 @@ class SerializedData {
}
byte* data_;
int size_;
uint32_t size_;
bool owns_data_;
private:
......
......@@ -12,6 +12,7 @@
#include "src/snapshot/partial-deserializer.h"
#include "src/snapshot/snapshot-source-sink.h"
#include "src/snapshot/startup-deserializer.h"
#include "src/utils.h"
#include "src/version.h"
namespace v8 {
......@@ -61,7 +62,7 @@ MaybeHandle<Context> Snapshot::NewContextFromSnapshot(
const v8::StartupData* blob = isolate->snapshot_blob();
bool can_rehash = ExtractRehashability(blob);
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);
MaybeHandle<Context> maybe_result = PartialDeserializer::DeserializeContext(
......@@ -103,93 +104,98 @@ void ProfileDeserialization(
v8::StartupData Snapshot::CreateSnapshotBlob(
const SnapshotData* startup_snapshot,
const std::vector<SnapshotData*>& context_snapshots, bool can_be_rehashed) {
int num_contexts = static_cast<int>(context_snapshots.size());
int startup_snapshot_offset = StartupSnapshotOffset(num_contexts);
int total_length = startup_snapshot_offset;
total_length += startup_snapshot->RawData().length();
uint32_t num_contexts = static_cast<uint32_t>(context_snapshots.size());
uint32_t startup_snapshot_offset = StartupSnapshotOffset(num_contexts);
uint32_t total_length = startup_snapshot_offset;
total_length += static_cast<uint32_t>(startup_snapshot->RawData().length());
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);
char* data = new char[total_length];
memcpy(data + kNumberOfContextsOffset, &num_contexts, kInt32Size);
int rehashability = can_be_rehashed ? 1 : 0;
memcpy(data + kRehashabilityOffset, &rehashability, kInt32Size);
int payload_offset = StartupSnapshotOffset(num_contexts);
int payload_length = startup_snapshot->RawData().length();
memcpy(data + payload_offset, startup_snapshot->RawData().start(),
payload_length);
SetHeaderValue(data, kNumberOfContextsOffset, num_contexts);
SetHeaderValue(data, kRehashabilityOffset, can_be_rehashed ? 1 : 0);
uint32_t payload_offset = StartupSnapshotOffset(num_contexts);
uint32_t payload_length =
static_cast<uint32_t>(startup_snapshot->RawData().length());
CopyBytes(data + payload_offset,
reinterpret_cast<const char*>(startup_snapshot->RawData().start()),
payload_length);
if (FLAG_profile_deserialization) {
PrintF("Snapshot blob consists of:\n%10d bytes for startup\n",
payload_length);
}
payload_offset += payload_length;
for (int i = 0; i < num_contexts; i++) {
memcpy(data + ContextSnapshotOffsetOffset(i), &payload_offset, kInt32Size);
for (uint32_t i = 0; i < num_contexts; i++) {
SetHeaderValue(data, ContextSnapshotOffsetOffset(i), payload_offset);
SnapshotData* context_snapshot = context_snapshots[i];
payload_length = context_snapshot->RawData().length();
memcpy(data + payload_offset, context_snapshot->RawData().start(),
payload_length);
CopyBytes(
data + payload_offset,
reinterpret_cast<const char*>(context_snapshot->RawData().start()),
payload_length);
if (FLAG_profile_deserialization) {
PrintF("%10d bytes for context #%d\n", payload_length, i);
}
payload_offset += payload_length;
}
v8::StartupData result = {data, total_length};
v8::StartupData result = {data, static_cast<int>(total_length)};
return result;
}
int Snapshot::ExtractNumContexts(const v8::StartupData* data) {
uint32_t Snapshot::ExtractNumContexts(const v8::StartupData* data) {
CHECK_LT(kNumberOfContextsOffset, data->raw_size);
int num_contexts;
memcpy(&num_contexts, data->data + kNumberOfContextsOffset, kInt32Size);
uint32_t num_contexts = GetHeaderValue(data, kNumberOfContextsOffset);
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) {
CHECK_LT(kRehashabilityOffset, data->raw_size);
int rehashability;
memcpy(&rehashability, data->data + kRehashabilityOffset, kInt32Size);
return rehashability != 0;
CHECK_LT(kRehashabilityOffset, static_cast<uint32_t>(data->raw_size));
return GetHeaderValue(data, kRehashabilityOffset) != 0;
}
Vector<const byte> Snapshot::ExtractStartupData(const v8::StartupData* data) {
int num_contexts = ExtractNumContexts(data);
int startup_offset = StartupSnapshotOffset(num_contexts);
uint32_t num_contexts = ExtractNumContexts(data);
uint32_t startup_offset = StartupSnapshotOffset(num_contexts);
CHECK_LT(startup_offset, data->raw_size);
int first_context_offset;
memcpy(&first_context_offset, data->data + ContextSnapshotOffsetOffset(0),
kInt32Size);
uint32_t first_context_offset = ExtractContextOffset(data, 0);
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 =
reinterpret_cast<const byte*>(data->data + startup_offset);
return Vector<const byte>(startup_data, startup_length);
}
Vector<const byte> Snapshot::ExtractContextData(const v8::StartupData* data,
int index) {
int num_contexts = ExtractNumContexts(data);
uint32_t index) {
uint32_t num_contexts = ExtractNumContexts(data);
CHECK_LT(index, num_contexts);
int context_offset;
memcpy(&context_offset, data->data + ContextSnapshotOffsetOffset(index),
kInt32Size);
int next_context_offset;
uint32_t context_offset = ExtractContextOffset(data, index);
uint32_t next_context_offset;
if (index == num_contexts - 1) {
next_context_offset = data->raw_size;
} else {
memcpy(&next_context_offset,
data->data + ContextSnapshotOffsetOffset(index + 1), kInt32Size);
next_context_offset = ExtractContextOffset(data, index + 1);
CHECK_LT(next_context_offset, data->raw_size);
}
const byte* context_data =
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);
}
......@@ -200,8 +206,10 @@ SnapshotData::SnapshotData(const Serializer* serializer) {
const std::vector<byte>* payload = serializer->sink()->data();
// Calculate sizes.
int reservation_size = static_cast<int>(reservations.size()) * kInt32Size;
int size = kHeaderSize + reservation_size + static_cast<int>(payload->size());
uint32_t reservation_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.
AllocateData(size);
......@@ -232,9 +240,10 @@ Vector<const SerializedData::Reservation> SnapshotData::Reservations() 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;
int length = GetHeaderValue(kPayloadLengthOffset);
uint32_t length = GetHeaderValue(kPayloadLengthOffset);
DCHECK_EQ(data_ + size_, payload + length);
return Vector<const byte>(payload, length);
}
......
......@@ -8,6 +8,8 @@
#include "src/snapshot/partial-serializer.h"
#include "src/snapshot/startup-serializer.h"
#include "src/utils.h"
namespace v8 {
namespace internal {
......@@ -46,9 +48,11 @@ class SnapshotData : public SerializedData {
// [4] payload length
// ... reservations
// ... serialized payload
static const int kNumReservationsOffset = kVersionHashOffset + kInt32Size;
static const int kPayloadLengthOffset = kNumReservationsOffset + kInt32Size;
static const int kHeaderSize = kPayloadLengthOffset + kInt32Size;
static const uint32_t kNumReservationsOffset =
kVersionHashOffset + kUInt32Size;
static const uint32_t kPayloadLengthOffset =
kNumReservationsOffset + kUInt32Size;
static const uint32_t kHeaderSize = kPayloadLengthOffset + kUInt32Size;
};
class Snapshot : public AllStatic {
......@@ -79,11 +83,20 @@ class Snapshot : public AllStatic {
#endif // DEBUG
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 Vector<const byte> ExtractStartupData(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:
// [0] number of contexts N
......@@ -96,17 +109,18 @@ class Snapshot : public AllStatic {
// ... context 0 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.
static const int kRehashabilityOffset = kNumberOfContextsOffset + kInt32Size;
static const int kFirstContextOffsetOffset =
kRehashabilityOffset + kInt32Size;
static const uint32_t kRehashabilityOffset =
kNumberOfContextsOffset + kUInt32Size;
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;
}
static int ContextSnapshotOffsetOffset(int index) {
static uint32_t ContextSnapshotOffsetOffset(int index) {
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