diff --git a/AUTHORS b/AUTHORS index c102d39cac..d6d9ffb1ed 100644 --- a/AUTHORS +++ b/AUTHORS @@ -118,6 +118,7 @@ Joel Stanley Johan Bergström Jonathan Liu Julien Brianceau +Junha Park JunHo Seo Junming Huang Kang-Hao (Kenny) Lu diff --git a/include/v8.h b/include/v8.h index 18d72f1630..64ffa1fcf4 100644 --- a/include/v8.h +++ b/include/v8.h @@ -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. diff --git a/src/api/api.cc b/src/api/api.cc index c9bda37991..d6f55b58cf 100644 --- a/src/api/api.cc +++ b/src/api/api.cc @@ -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); } diff --git a/src/snapshot/snapshot.cc b/src/snapshot/snapshot.cc index 6c129a846a..fd0866619c 100644 --- a/src/snapshot/snapshot.cc +++ b/src/snapshot/snapshot.cc @@ -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(data->raw_size)); + Version::GetString(Vector(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 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(data->raw_size)); - Version::GetString(Vector(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(data->raw_size)); + Version::GetString(Vector(version, kVersionStringLength)); FATAL( "Version mismatch between V8 binary and snapshot.\n" "# V8 binary version: %.*s\n" diff --git a/src/snapshot/snapshot.h b/src/snapshot/snapshot.h index e0ea02681c..016e51799b 100644 --- a/src/snapshot/snapshot.h +++ b/src/snapshot/snapshot.h @@ -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();