Commit 7b70036e authored by Marja Hölttä's avatar Marja Hölttä Committed by V8 LUCI CQ

[web snapshots] Add a magic number to the binary format

Bug: v8:11525
Change-Id: I848167e13c7aa47d712a4315902eb8748e86c949
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3217192
Commit-Queue: Marja Hölttä <marja@chromium.org>
Reviewed-by: 's avatarCamillo Bruni <cbruni@chromium.org>
Cr-Commit-Position: refs/heads/main@{#77317}
parent 12ecb4f5
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
namespace v8 { namespace v8 {
namespace internal { namespace internal {
constexpr uint8_t WebSnapshotSerializerDeserializer::kMagicNumber[4];
// When encountering an error during deserializing, we note down the error but // When encountering an error during deserializing, we note down the error but
// don't bail out from processing the snapshot further. This is to speed up // don't bail out from processing the snapshot further. This is to speed up
// deserialization; the error case is now slower since we don't bail out, but // deserialization; the error case is now slower since we don't bail out, but
...@@ -258,6 +260,7 @@ void WebSnapshotSerializer::SerializePendingItems() { ...@@ -258,6 +260,7 @@ void WebSnapshotSerializer::SerializePendingItems() {
} }
// Format (full snapshot): // Format (full snapshot):
// - Magic number (4 bytes)
// - String count // - String count
// - For each string: // - For each string:
// - Serialized string // - Serialized string
...@@ -282,16 +285,16 @@ void WebSnapshotSerializer::WriteSnapshot(uint8_t*& buffer, ...@@ -282,16 +285,16 @@ void WebSnapshotSerializer::WriteSnapshot(uint8_t*& buffer,
ValueSerializer total_serializer(isolate_, nullptr); ValueSerializer total_serializer(isolate_, nullptr);
size_t needed_size = size_t needed_size =
string_serializer_.buffer_size_ + map_serializer_.buffer_size_ + sizeof(kMagicNumber) + string_serializer_.buffer_size_ +
context_serializer_.buffer_size_ + function_serializer_.buffer_size_ + map_serializer_.buffer_size_ + context_serializer_.buffer_size_ +
class_serializer_.buffer_size_ + array_serializer_.buffer_size_ + function_serializer_.buffer_size_ + class_serializer_.buffer_size_ +
object_serializer_.buffer_size_ + export_serializer_.buffer_size_ + array_serializer_.buffer_size_ + object_serializer_.buffer_size_ +
8 * sizeof(uint32_t); export_serializer_.buffer_size_ + 8 * sizeof(uint32_t);
if (total_serializer.ExpandBuffer(needed_size).IsNothing()) { if (total_serializer.ExpandBuffer(needed_size).IsNothing()) {
Throw("Web snapshot: Out of memory"); Throw("Web snapshot: Out of memory");
return; return;
} }
total_serializer.WriteRawBytes(kMagicNumber, 4);
total_serializer.WriteUint32(static_cast<uint32_t>(string_count())); total_serializer.WriteUint32(static_cast<uint32_t>(string_count()));
total_serializer.WriteRawBytes(string_serializer_.buffer_, total_serializer.WriteRawBytes(string_serializer_.buffer_,
string_serializer_.buffer_size_); string_serializer_.buffer_size_);
...@@ -778,6 +781,14 @@ bool WebSnapshotDeserializer::UseWebSnapshot(const uint8_t* data, ...@@ -778,6 +781,14 @@ bool WebSnapshotDeserializer::UseWebSnapshot(const uint8_t* data,
deserializer_.reset(new ValueDeserializer(isolate_, data, buffer_size)); deserializer_.reset(new ValueDeserializer(isolate_, data, buffer_size));
deferred_references_ = ArrayList::New(isolate_, 30); deferred_references_ = ArrayList::New(isolate_, 30);
const void* magic_bytes;
if (!deserializer_->ReadRawBytes(sizeof(kMagicNumber), &magic_bytes) ||
memcmp(magic_bytes, kMagicNumber, sizeof(kMagicNumber)) != 0) {
Throw("Web snapshot: Invalid magic number");
return false;
}
DeserializeStrings(); DeserializeStrings();
DeserializeMaps(); DeserializeMaps();
DeserializeContexts(); DeserializeContexts();
......
...@@ -56,6 +56,8 @@ class WebSnapshotSerializerDeserializer { ...@@ -56,6 +56,8 @@ class WebSnapshotSerializerDeserializer {
REGEXP REGEXP
}; };
static constexpr uint8_t kMagicNumber[4] = {'+', '+', '+', ';'};
enum ContextType : uint8_t { FUNCTION, BLOCK }; enum ContextType : uint8_t { FUNCTION, BLOCK };
enum PropertyAttributesType : uint8_t { DEFAULT, CUSTOM }; enum PropertyAttributesType : uint8_t { DEFAULT, CUSTOM };
......
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