Skip to content

Commit e0a109c

Browse files
joyeecheungCommit Bot
authored andcommitted
[api] Implement StartupData::CanBeRehashed() for the snapshot blob
This enables the embedder to check if the snapshot generated from SnapshotCreator::CreateBlob() can be rehashed and the seed can be recomputed during deserialization. The lack of this functionality resulted in a temporary vunerability in Node.js: nodejs/node#27365 Change-Id: I88d52337217c40f79c26438be3c87d2db874d980 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1578661 Commit-Queue: Joyee Cheung <[email protected]> Reviewed-by: Yang Guo <[email protected]> Cr-Commit-Position: refs/heads/master@{#61175}
1 parent 87b3416 commit e0a109c

File tree

5 files changed

+19
-2
lines changed

5 files changed

+19
-2
lines changed

include/v8.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8581,6 +8581,13 @@ class V8_EXPORT Isolate {
85818581

85828582
class V8_EXPORT StartupData {
85838583
public:
8584+
/**
8585+
* Whether the data created can be rehashed and and the hash seed can be
8586+
* recomputed when deserialized.
8587+
* Only valid for StartupData returned by SnapshotCreator::CreateBlob().
8588+
*/
8589+
bool CanBeRehashed() const;
8590+
85848591
const char* data;
85858592
int raw_size;
85868593
};

src/api.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,11 @@ StartupData SnapshotCreator::CreateBlob(
887887
return result;
888888
}
889889

890+
bool StartupData::CanBeRehashed() const {
891+
DCHECK(i::Snapshot::VerifyChecksum(this));
892+
return i::Snapshot::ExtractRehashability(this);
893+
}
894+
890895
void V8::SetDcheckErrorHandler(DcheckErrorCallback that) {
891896
v8::base::SetDcheckFunction(that);
892897
}

src/snapshot/snapshot-common.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,9 @@ uint32_t Snapshot::ExtractContextOffset(const v8::StartupData* data,
230230

231231
bool Snapshot::ExtractRehashability(const v8::StartupData* data) {
232232
CHECK_LT(kRehashabilityOffset, static_cast<uint32_t>(data->raw_size));
233-
return GetHeaderValue(data, kRehashabilityOffset) != 0;
233+
uint32_t rehashability = GetHeaderValue(data, kRehashabilityOffset);
234+
CHECK_IMPLIES(rehashability != 0, rehashability == 1);
235+
return rehashability != 0;
234236
}
235237

236238
namespace {

src/snapshot/snapshot.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ class Snapshot : public AllStatic {
8686
static bool SnapshotIsValid(const v8::StartupData* snapshot_blob);
8787
#endif // DEBUG
8888

89+
static bool ExtractRehashability(const v8::StartupData* data);
90+
8991
private:
9092
static uint32_t ExtractNumContexts(const v8::StartupData* data);
9193
static uint32_t ExtractContextOffset(const v8::StartupData* data,
9294
uint32_t index);
93-
static bool ExtractRehashability(const v8::StartupData* data);
9495
static Vector<const byte> ExtractStartupData(const v8::StartupData* data);
9596
static Vector<const byte> ExtractReadOnlyData(const v8::StartupData* data);
9697
static Vector<const byte> ExtractContextData(const v8::StartupData* data,

test/cctest/test-serialize.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3721,6 +3721,7 @@ UNINITIALIZED_TEST(ReinitializeHashSeedNotRehashable) {
37213721
}
37223722
blob =
37233723
creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear);
3724+
CHECK(!blob.CanBeRehashed());
37243725
}
37253726

37263727
i::FLAG_hash_seed = 1337;
@@ -3786,6 +3787,7 @@ UNINITIALIZED_TEST(ReinitializeHashSeedRehashable) {
37863787
}
37873788
blob =
37883789
creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear);
3790+
CHECK(blob.CanBeRehashed());
37893791
}
37903792

37913793
i::FLAG_hash_seed = 1337;

0 commit comments

Comments
 (0)