Commit c3112fc2 authored by Junha Park's avatar Junha Park Committed by Commit Bot

Added API to verify version match on snapshot blob

This patch added an IsValid method to StartupData which returns a
boolean upon verifying a given snapshot matches the v8 version.
Embedders can use this API now to check snapshots' versions.

This was originally done by Snapshot::CheckVersion, which now simply
runs Startup::IsValid.

Bug: v8:8104
Change-Id: If555bcc55de4a05adf61798cd58d9ea8c8a71302
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2178091
Commit-Queue: Yang Guo <yangguo@chromium.org>
Reviewed-by: 's avatarYang Guo <yangguo@chromium.org>
Reviewed-by: 's avatarJakob Gruber <jgruber@chromium.org>
Auto-Submit: Junha Park <jpark3@scu.edu>
Cr-Commit-Position: refs/heads/master@{#67951}
parent dcc92be7
......@@ -118,6 +118,7 @@ Joel Stanley <joel@jms.id.au>
Johan Bergström <johan@bergstroem.nu>
Jonathan Liu <net147@gmail.com>
Julien Brianceau <jbriance@cisco.com>
Junha Park <jpark3@scu.edu>
JunHo Seo <sejunho@gmail.com>
Junming Huang <kiminghjm@gmail.com>
Kang-Hao (Kenny) Lu <kennyluck@csail.mit.edu>
......
......@@ -9499,12 +9499,16 @@ class V8_EXPORT StartupData {
* Only valid for StartupData returned by SnapshotCreator::CreateBlob().
*/
bool CanBeRehashed() const;
/**
* Allows embedders to verify whether the data is valid for the current
* V8 instance.
*/
bool IsValid() const;
const char* data;
int raw_size;
};
/**
* EntropySource is used as a callback function when v8 needs a source
* of entropy.
......
......@@ -823,6 +823,8 @@ bool StartupData::CanBeRehashed() const {
return i::Snapshot::ExtractRehashability(this);
}
bool StartupData::IsValid() const { return i::Snapshot::VersionIsValid(this); }
void V8::SetDcheckErrorHandler(DcheckErrorCallback that) {
v8::base::SetDcheckFunction(that);
}
......
......@@ -128,6 +128,17 @@ bool Snapshot::HasContextSnapshot(Isolate* isolate, size_t index) {
return index < num_contexts;
}
bool Snapshot::VersionIsValid(const v8::StartupData* data) {
char version[SnapshotImpl::kVersionStringLength];
memset(version, 0, SnapshotImpl::kVersionStringLength);
CHECK_LT(
SnapshotImpl::kVersionStringOffset + SnapshotImpl::kVersionStringLength,
static_cast<uint32_t>(data->raw_size));
Version::GetString(Vector<char>(version, SnapshotImpl::kVersionStringLength));
return strncmp(version, data->data + SnapshotImpl::kVersionStringOffset,
SnapshotImpl::kVersionStringLength) == 0;
}
bool Snapshot::Initialize(Isolate* isolate) {
if (!isolate->snapshot_available()) return false;
RuntimeCallTimerScope rcs_timer(isolate,
......@@ -600,13 +611,12 @@ Vector<const byte> SnapshotImpl::ExtractContextData(const v8::StartupData* data,
}
void SnapshotImpl::CheckVersion(const v8::StartupData* data) {
char version[kVersionStringLength];
memset(version, 0, kVersionStringLength);
CHECK_LT(kVersionStringOffset + kVersionStringLength,
static_cast<uint32_t>(data->raw_size));
Version::GetString(Vector<char>(version, kVersionStringLength));
if (strncmp(version, data->data + kVersionStringOffset,
kVersionStringLength) != 0) {
if (!Snapshot::VersionIsValid(data)) {
char version[kVersionStringLength];
memset(version, 0, kVersionStringLength);
CHECK_LT(kVersionStringOffset + kVersionStringLength,
static_cast<uint32_t>(data->raw_size));
Version::GetString(Vector<char>(version, kVersionStringLength));
FATAL(
"Version mismatch between V8 binary and snapshot.\n"
"# V8 binary version: %.*s\n"
......
......@@ -91,6 +91,7 @@ class Snapshot : public AllStatic {
static bool EmbedsScript(Isolate* isolate);
V8_EXPORT_PRIVATE static bool VerifyChecksum(const v8::StartupData* data);
static bool ExtractRehashability(const v8::StartupData* data);
static bool VersionIsValid(const v8::StartupData* data);
// To be implemented by the snapshot source.
static const v8::StartupData* DefaultSnapshotBlob();
......
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