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