Commit 964d1759 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

Make SerializedData::kMagicNumber isolate-independent

We currently compute this value from the size of the external table,
which we get from the Isolate. This size is isolate-independent though,
so it can just be a constant.

R=mstarzinger@chromium.org

Bug: chromium:912043
Change-Id: If1c09a56b1a985b855f5b65818322979c194d772
Reviewed-on: https://chromium-review.googlesource.com/c/1362954Reviewed-by: 's avatarJakob Gruber <jgruber@chromium.org>
Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58045}
parent 148ef606
...@@ -63,7 +63,7 @@ void V8NameConverter::InitExternalRefsCache() const { ...@@ -63,7 +63,7 @@ void V8NameConverter::InitExternalRefsCache() const {
isolate_->root_register_addressable_region(); isolate_->root_register_addressable_region();
Address isolate_root = isolate_->isolate_root(); Address isolate_root = isolate_->isolate_root();
for (uint32_t i = 0; i < external_reference_table->size(); i++) { for (uint32_t i = 0; i < ExternalReferenceTable::kSize; i++) {
Address address = external_reference_table->address(i); Address address = external_reference_table->address(i);
if (addressable_region.contains(address)) { if (addressable_region.contains(address)) {
int offset = static_cast<int>(address - isolate_root); int offset = static_cast<int>(address - isolate_root);
...@@ -119,7 +119,7 @@ const char* V8NameConverter::RootRelativeName(int offset) const { ...@@ -119,7 +119,7 @@ const char* V8NameConverter::RootRelativeName(int offset) const {
const int kRootsTableStart = IsolateData::roots_table_offset(); const int kRootsTableStart = IsolateData::roots_table_offset();
const unsigned kRootsTableSize = sizeof(RootsTable); const unsigned kRootsTableSize = sizeof(RootsTable);
const int kExtRefsTableStart = IsolateData::external_reference_table_offset(); const int kExtRefsTableStart = IsolateData::external_reference_table_offset();
const unsigned kExtRefsTableSize = ExternalReferenceTable::SizeInBytes(); const unsigned kExtRefsTableSize = ExternalReferenceTable::kSizeInBytes;
const int kBuiltinsTableStart = IsolateData::builtins_table_offset(); const int kBuiltinsTableStart = IsolateData::builtins_table_offset();
const unsigned kBuiltinsTableSize = Builtins::builtin_count * kPointerSize; const unsigned kBuiltinsTableSize = Builtins::builtin_count * kPointerSize;
...@@ -140,7 +140,7 @@ const char* V8NameConverter::RootRelativeName(int offset) const { ...@@ -140,7 +140,7 @@ const char* V8NameConverter::RootRelativeName(int offset) const {
uint32_t offset_in_extref_table = offset - kExtRefsTableStart; uint32_t offset_in_extref_table = offset - kExtRefsTableStart;
// Fail safe in the unlikely case of an arbitrary root-relative offset. // Fail safe in the unlikely case of an arbitrary root-relative offset.
if (offset_in_extref_table % ExternalReferenceTable::EntrySize() != 0) { if (offset_in_extref_table % ExternalReferenceTable::kEntrySize != 0) {
return nullptr; return nullptr;
} }
......
...@@ -42,8 +42,9 @@ class ExternalReferenceTable { ...@@ -42,8 +42,9 @@ class ExternalReferenceTable {
kBuiltinsReferenceCount + kRuntimeReferenceCount + kBuiltinsReferenceCount + kRuntimeReferenceCount +
kIsolateAddressReferenceCount + kAccessorReferenceCount + kIsolateAddressReferenceCount + kAccessorReferenceCount +
kStubCacheReferenceCount; kStubCacheReferenceCount;
static constexpr uint32_t kEntrySize = 2 * kPointerSize;
static constexpr uint32_t kSizeInBytes = kSize * kEntrySize + 2 * kUInt32Size;
static constexpr uint32_t size() { return static_cast<uint32_t>(kSize); }
Address address(uint32_t i) { return refs_[i].address; } Address address(uint32_t i) { return refs_[i].address; }
const char* name(uint32_t i) { return refs_[i].name; } const char* name(uint32_t i) { return refs_[i].name; }
...@@ -51,29 +52,19 @@ class ExternalReferenceTable { ...@@ -51,29 +52,19 @@ class ExternalReferenceTable {
static const char* ResolveSymbol(void* address); static const char* ResolveSymbol(void* address);
static constexpr uint32_t EntrySize() {
return sizeof(ExternalReferenceEntry);
}
static constexpr uint32_t OffsetOfEntry(uint32_t i) { static constexpr uint32_t OffsetOfEntry(uint32_t i) {
// Used in CodeAssembler::LookupExternalReference. // Used in CodeAssembler::LookupExternalReference.
STATIC_ASSERT(offsetof(ExternalReferenceEntry, address) == 0); STATIC_ASSERT(offsetof(ExternalReferenceEntry, address) == 0);
return i * EntrySize(); return i * kEntrySize;
} }
const char* NameFromOffset(uint32_t offset) { const char* NameFromOffset(uint32_t offset) {
DCHECK_EQ(offset % EntrySize(), 0); DCHECK_EQ(offset % kEntrySize, 0);
DCHECK_LT(offset, SizeInBytes()); DCHECK_LT(offset, kSizeInBytes);
int index = offset / EntrySize(); int index = offset / kEntrySize;
return name(index); return name(index);
} }
static constexpr uint32_t SizeInBytes() {
STATIC_ASSERT(OffsetOfEntry(size()) + 2 * kUInt32Size ==
sizeof(ExternalReferenceTable));
return OffsetOfEntry(size()) + 2 * kUInt32Size;
}
ExternalReferenceTable() = default; ExternalReferenceTable() = default;
void Init(Isolate* isolate); void Init(Isolate* isolate);
...@@ -86,6 +77,7 @@ class ExternalReferenceTable { ...@@ -86,6 +77,7 @@ class ExternalReferenceTable {
ExternalReferenceEntry(Address address, const char* name) ExternalReferenceEntry(Address address, const char* name)
: address(address), name(name) {} : address(address), name(name) {}
}; };
STATIC_ASSERT(kEntrySize == sizeof(ExternalReferenceEntry));
void Add(Address address, const char* name, int* index); void Add(Address address, const char* name, int* index);
...@@ -103,6 +95,9 @@ class ExternalReferenceTable { ...@@ -103,6 +95,9 @@ class ExternalReferenceTable {
DISALLOW_COPY_AND_ASSIGN(ExternalReferenceTable); DISALLOW_COPY_AND_ASSIGN(ExternalReferenceTable);
}; };
STATIC_ASSERT(ExternalReferenceTable::kSizeInBytes ==
sizeof(ExternalReferenceTable));
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
#endif // V8_EXTERNAL_REFERENCE_TABLE_H_ #endif // V8_EXTERNAL_REFERENCE_TABLE_H_
...@@ -97,21 +97,21 @@ class IsolateData final { ...@@ -97,21 +97,21 @@ class IsolateData final {
private: private:
// Static layout definition. // Static layout definition.
#define FIELDS(V) \ #define FIELDS(V) \
V(kEmbedderDataOffset, Internals::kNumIsolateDataSlots* kPointerSize) \ V(kEmbedderDataOffset, Internals::kNumIsolateDataSlots* kPointerSize) \
V(kExternalMemoryOffset, kInt64Size) \ V(kExternalMemoryOffset, kInt64Size) \
V(kExternalMemoryLlimitOffset, kInt64Size) \ V(kExternalMemoryLlimitOffset, kInt64Size) \
V(kExternalMemoryAtLastMarkCompactOffset, kInt64Size) \ V(kExternalMemoryAtLastMarkCompactOffset, kInt64Size) \
V(kRootsTableOffset, RootsTable::kEntriesCount* kPointerSize) \ V(kRootsTableOffset, RootsTable::kEntriesCount* kPointerSize) \
V(kExternalReferenceTableOffset, ExternalReferenceTable::SizeInBytes()) \ V(kExternalReferenceTableOffset, ExternalReferenceTable::kSizeInBytes) \
V(kBuiltinsTableOffset, Builtins::builtin_count* kPointerSize) \ V(kBuiltinsTableOffset, Builtins::builtin_count* kPointerSize) \
V(kVirtualCallTargetRegisterOffset, kPointerSize) \ V(kVirtualCallTargetRegisterOffset, kPointerSize) \
V(kFastCCallCallerFPOffset, kPointerSize) \ V(kFastCCallCallerFPOffset, kPointerSize) \
V(kFastCCallCallerPCOffset, kPointerSize) \ V(kFastCCallCallerPCOffset, kPointerSize) \
/* This padding aligns IsolateData size by 8 bytes. */ \ /* This padding aligns IsolateData size by 8 bytes. */ \
V(kPaddingOffset, \ V(kPaddingOffset, \
8 + RoundUp<8>(static_cast<int>(kPaddingOffset)) - kPaddingOffset) \ 8 + RoundUp<8>(static_cast<int>(kPaddingOffset)) - kPaddingOffset) \
/* Total size. */ \ /* Total size. */ \
V(kSize, 0) V(kSize, 0)
DEFINE_FIELD_OFFSET_CONSTANTS(0, FIELDS) DEFINE_FIELD_OFFSET_CONSTANTS(0, FIELDS)
......
...@@ -351,7 +351,7 @@ SerializedCodeData::SerializedCodeData(const std::vector<byte>* payload, ...@@ -351,7 +351,7 @@ SerializedCodeData::SerializedCodeData(const std::vector<byte>* payload,
memset(data_, 0, padded_payload_offset); memset(data_, 0, padded_payload_offset);
// Set header values. // Set header values.
SetMagicNumber(cs->isolate()); SetMagicNumber();
SetHeaderValue(kVersionHashOffset, Version::Hash()); SetHeaderValue(kVersionHashOffset, Version::Hash());
SetHeaderValue(kSourceHashOffset, cs->source_hash()); SetHeaderValue(kSourceHashOffset, cs->source_hash());
SetHeaderValue(kCpuFeaturesOffset, SetHeaderValue(kCpuFeaturesOffset,
...@@ -387,7 +387,7 @@ SerializedCodeData::SanityCheckResult SerializedCodeData::SanityCheck( ...@@ -387,7 +387,7 @@ SerializedCodeData::SanityCheckResult SerializedCodeData::SanityCheck(
Isolate* isolate, uint32_t expected_source_hash) const { Isolate* isolate, uint32_t expected_source_hash) const {
if (this->size_ < kHeaderSize) return INVALID_HEADER; if (this->size_ < kHeaderSize) return INVALID_HEADER;
uint32_t magic_number = GetMagicNumber(); uint32_t magic_number = GetMagicNumber();
if (magic_number != ComputeMagicNumber(isolate)) return MAGIC_NUMBER_MISMATCH; if (magic_number != kMagicNumber) return MAGIC_NUMBER_MISMATCH;
uint32_t version_hash = GetHeaderValue(kVersionHashOffset); uint32_t version_hash = GetHeaderValue(kVersionHashOffset);
uint32_t source_hash = GetHeaderValue(kSourceHashOffset); uint32_t source_hash = GetHeaderValue(kSourceHashOffset);
uint32_t cpu_features = GetHeaderValue(kCpuFeaturesOffset); uint32_t cpu_features = GetHeaderValue(kCpuFeaturesOffset);
......
...@@ -91,8 +91,7 @@ void Deserializer::Initialize(Isolate* isolate) { ...@@ -91,8 +91,7 @@ void Deserializer::Initialize(Isolate* isolate) {
} }
} }
#endif // DEBUG #endif // DEBUG
CHECK_EQ(magic_number_, CHECK_EQ(magic_number_, SerializedData::kMagicNumber);
SerializedData::ComputeMagicNumber(external_reference_table_));
} }
void Deserializer::Rehash() { void Deserializer::Rehash() {
......
...@@ -24,7 +24,7 @@ ExternalReferenceEncoder::ExternalReferenceEncoder(Isolate* isolate) { ...@@ -24,7 +24,7 @@ ExternalReferenceEncoder::ExternalReferenceEncoder(Isolate* isolate) {
isolate->set_external_reference_map(map_); isolate->set_external_reference_map(map_);
// Add V8's external references. // Add V8's external references.
ExternalReferenceTable* table = isolate->external_reference_table(); ExternalReferenceTable* table = isolate->external_reference_table();
for (uint32_t i = 0; i < table->size(); ++i) { for (uint32_t i = 0; i < ExternalReferenceTable::kSize; ++i) {
Address addr = table->address(i); Address addr = table->address(i);
// Ignore duplicate references. // Ignore duplicate references.
// This can happen due to ICF. See http://crbug.com/726896. // This can happen due to ICF. See http://crbug.com/726896.
...@@ -102,9 +102,7 @@ void SerializedData::AllocateData(uint32_t size) { ...@@ -102,9 +102,7 @@ void SerializedData::AllocateData(uint32_t size) {
} }
// static // static
uint32_t SerializedData::ComputeMagicNumber(Isolate* isolate) { constexpr uint32_t SerializedData::kMagicNumber;
return ComputeMagicNumber(isolate->external_reference_table());
}
// The partial snapshot cache is terminated by undefined. We visit the // The partial snapshot cache is terminated by undefined. We visit the
// partial snapshot... // partial snapshot...
......
...@@ -322,12 +322,9 @@ class SerializedData { ...@@ -322,12 +322,9 @@ class SerializedData {
class ChunkSizeBits : public BitField<uint32_t, 0, 31> {}; class ChunkSizeBits : public BitField<uint32_t, 0, 31> {};
class IsLastChunkBits : public BitField<bool, 31, 1> {}; class IsLastChunkBits : public BitField<bool, 31, 1> {};
static uint32_t ComputeMagicNumber(ExternalReferenceTable* table) { static constexpr uint32_t kMagicNumberOffset = 0;
uint32_t external_refs = table->size(); static constexpr uint32_t kMagicNumber =
return 0xC0DE0000 ^ external_refs; 0xC0DE0000 ^ ExternalReferenceTable::kSize;
}
static const uint32_t kMagicNumberOffset = 0;
protected: protected:
void SetHeaderValue(uint32_t offset, uint32_t value) { void SetHeaderValue(uint32_t offset, uint32_t value) {
...@@ -341,11 +338,7 @@ class SerializedData { ...@@ -341,11 +338,7 @@ class SerializedData {
void AllocateData(uint32_t size); void AllocateData(uint32_t size);
static uint32_t ComputeMagicNumber(Isolate* isolate); void SetMagicNumber() { SetHeaderValue(kMagicNumberOffset, kMagicNumber); }
void SetMagicNumber(Isolate* isolate) {
SetHeaderValue(kMagicNumberOffset, ComputeMagicNumber(isolate));
}
byte* data_; byte* data_;
uint32_t size_; uint32_t size_;
......
...@@ -316,7 +316,7 @@ SnapshotData::SnapshotData(const Serializer* serializer) { ...@@ -316,7 +316,7 @@ SnapshotData::SnapshotData(const Serializer* serializer) {
memset(data_, 0, padded_payload_offset); memset(data_, 0, padded_payload_offset);
// Set header values. // Set header values.
SetMagicNumber(serializer->isolate()); SetMagicNumber();
SetHeaderValue(kNumReservationsOffset, static_cast<int>(reservations.size())); SetHeaderValue(kNumReservationsOffset, static_cast<int>(reservations.size()));
SetHeaderValue(kPayloadLengthOffset, static_cast<int>(payload->size())); SetHeaderValue(kPayloadLengthOffset, static_cast<int>(payload->size()));
......
...@@ -71,9 +71,7 @@ class WasmStreaming::WasmStreamingImpl { ...@@ -71,9 +71,7 @@ class WasmStreaming::WasmStreamingImpl {
} }
bool SetCompiledModuleBytes(const uint8_t* bytes, size_t size) { bool SetCompiledModuleBytes(const uint8_t* bytes, size_t size) {
if (!i::wasm::IsSupportedVersion(reinterpret_cast<i::Isolate*>(isolate_), if (!i::wasm::IsSupportedVersion({bytes, size})) return false;
{bytes, size}))
return false;
return streaming_decoder_->SetCompiledModuleBytes({bytes, size}); return streaming_decoder_->SetCompiledModuleBytes({bytes, size});
} }
......
...@@ -119,9 +119,8 @@ class Reader { ...@@ -119,9 +119,8 @@ class Reader {
constexpr size_t kVersionSize = 4 * sizeof(uint32_t); constexpr size_t kVersionSize = 4 * sizeof(uint32_t);
void WriteVersion(Isolate* isolate, Writer* writer) { void WriteVersion(Writer* writer) {
writer->Write( writer->Write(SerializedData::kMagicNumber);
SerializedData::ComputeMagicNumber(isolate->external_reference_table()));
writer->Write(Version::Hash()); writer->Write(Version::Hash());
writer->Write(static_cast<uint32_t>(CpuFeatures::SupportedFeatures())); writer->Write(static_cast<uint32_t>(CpuFeatures::SupportedFeatures()));
writer->Write(FlagList::Hash()); writer->Write(FlagList::Hash());
...@@ -244,7 +243,7 @@ NativeModuleSerializer::NativeModuleSerializer( ...@@ -244,7 +243,7 @@ NativeModuleSerializer::NativeModuleSerializer(
wasm_stub_targets_lookup_.insert(std::make_pair(addr, i)); wasm_stub_targets_lookup_.insert(std::make_pair(addr, i));
} }
ExternalReferenceTable* table = isolate_->external_reference_table(); ExternalReferenceTable* table = isolate_->external_reference_table();
for (uint32_t i = 0; i < table->size(); ++i) { for (uint32_t i = 0; i < ExternalReferenceTable::kSize; ++i) {
Address addr = table->address(i); Address addr = table->address(i);
reference_table_lookup_.insert(std::make_pair(addr, i)); reference_table_lookup_.insert(std::make_pair(addr, i));
} }
...@@ -395,7 +394,7 @@ bool WasmSerializer::SerializeNativeModule(Vector<byte> buffer) const { ...@@ -395,7 +394,7 @@ bool WasmSerializer::SerializeNativeModule(Vector<byte> buffer) const {
if (buffer.size() < measured_size) return false; if (buffer.size() < measured_size) return false;
Writer writer(buffer); Writer writer(buffer);
WriteVersion(isolate_, &writer); WriteVersion(&writer);
if (!serializer.Write(&writer)) return false; if (!serializer.Write(&writer)) return false;
DCHECK_EQ(measured_size, writer.bytes_written()); DCHECK_EQ(measured_size, writer.bytes_written());
...@@ -531,22 +530,19 @@ bool NativeModuleDeserializer::ReadCode(uint32_t fn_index, Reader* reader) { ...@@ -531,22 +530,19 @@ bool NativeModuleDeserializer::ReadCode(uint32_t fn_index, Reader* reader) {
return true; return true;
} }
bool IsSupportedVersion(Isolate* isolate, Vector<const byte> version) { bool IsSupportedVersion(Vector<const byte> version) {
if (version.size() < kVersionSize) return false; if (version.size() < kVersionSize) return false;
byte current_version[kVersionSize]; byte current_version[kVersionSize];
Writer writer({current_version, kVersionSize}); Writer writer({current_version, kVersionSize});
WriteVersion(isolate, &writer); WriteVersion(&writer);
return memcmp(version.start(), current_version, kVersionSize) == 0; return memcmp(version.start(), current_version, kVersionSize) == 0;
} }
MaybeHandle<WasmModuleObject> DeserializeNativeModule( MaybeHandle<WasmModuleObject> DeserializeNativeModule(
Isolate* isolate, Vector<const byte> data, Vector<const byte> wire_bytes) { Isolate* isolate, Vector<const byte> data, Vector<const byte> wire_bytes) {
if (!IsWasmCodegenAllowed(isolate, isolate->native_context())) { if (!IsWasmCodegenAllowed(isolate, isolate->native_context())) return {};
return {}; if (!IsSupportedVersion(data)) return {};
}
if (!IsSupportedVersion(isolate, data)) {
return {};
}
// TODO(titzer): module features should be part of the serialization format. // TODO(titzer): module features should be part of the serialization format.
WasmFeatures enabled_features = WasmFeaturesFromIsolate(isolate); WasmFeatures enabled_features = WasmFeaturesFromIsolate(isolate);
ModuleResult decode_result = DecodeWasmModule( ModuleResult decode_result = DecodeWasmModule(
......
...@@ -33,7 +33,7 @@ class WasmSerializer { ...@@ -33,7 +33,7 @@ class WasmSerializer {
// Support for deserializing WebAssembly {NativeModule} objects. // Support for deserializing WebAssembly {NativeModule} objects.
// Checks the version header of the data against the current version. // Checks the version header of the data against the current version.
bool IsSupportedVersion(Isolate* isolate, Vector<const byte> data); bool IsSupportedVersion(Vector<const byte> data);
// Deserializes the given data to create a compiled Wasm module. // Deserializes the given data to create a compiled Wasm module.
MaybeHandle<WasmModuleObject> DeserializeNativeModule( MaybeHandle<WasmModuleObject> DeserializeNativeModule(
......
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