Skip to content

Commit 39fc019

Browse files
committed
for mac builds fake the memory mapped file (by using recordreplay::ReadSystemFileContents)
1 parent 44a16d0 commit 39fc019

5 files changed

Lines changed: 41 additions & 1 deletion

File tree

base/files/memory_mapped_file.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ bool MemoryMappedFile::IsValid() const {
127127
return data_ != nullptr;
128128
}
129129

130+
void MemoryMappedFile::FakeFromData(const uint8_t* data, size_t length) {
131+
DCHECK(!IsValid());
132+
data_ = const_cast<uint8_t*>(data);
133+
length_ = length;
134+
}
135+
130136
// static
131137
void MemoryMappedFile::CalculateVMAlignedBoundaries(int64_t start,
132138
size_t size,

base/files/memory_mapped_file.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ class BASE_EXPORT MemoryMappedFile {
112112
// Is file_ a valid file handle that points to an open, memory mapped file?
113113
bool IsValid() const;
114114

115+
void FakeFromData(const uint8_t* data, size_t length);
116+
115117
private:
116118
// Given the arbitrarily aligned memory region [start, size], returns the
117119
// boundaries of the region aligned to the granularity specified by the OS,

base/record_replay.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ namespace recordreplay {
4242
Macro(V8IsMainThread, (), (), bool, false) \
4343
Macro(V8RecordReplayIsInReplayCode, \
4444
(const char* why), (why), bool, false) \
45-
Macro(V8RecordReplayHadMismatch, (), (), bool, false)
45+
Macro(V8RecordReplayHadMismatch, (), (), bool, false) \
46+
Macro(V8RecordReplayReadSystemFileContents, \
47+
(bool aRelativeToApplication, const char* aPath, size_t* aLength), \
48+
(aRelativeToApplication, aPath, aLength), char*, nullptr)
4649

4750
#define ForEachV8APIVoid(Macro) \
4851
Macro(V8RecordReplayAssertVA, \

base/record_replay.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ uintptr_t RecordReplayValue(const char* why, uintptr_t v);
4545
void RecordReplayBytes(const char* why, void* buf, size_t size);
4646
void RecordReplayString(const char* why, std::string& text);
4747

48+
char* ReadSystemFileContents(bool aRelativeToApplication, const char* aPath, size_t* aLength);
49+
4850
int CreateOrderedLock(const char* name);
4951
void OrderedLock(int lock);
5052
void OrderedUnlock(int lock);

gin/v8_initializer.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,20 @@ void GetV8FilePath(const char* file_name, base::FilePath* path_out) {
137137
#endif
138138
}
139139

140+
#if BUILDFLAG(IS_MAC)
141+
bool FakeMapV8File(const char* aPath, base::MemoryMappedFile** mmapped_file_out) {
142+
std::unique_ptr<base::MemoryMappedFile> mmapped_file(
143+
new base::MemoryMappedFile());
144+
size_t length;
145+
uint8_t* data = reinterpret_cast<uint8_t*>(recordreplay::ReadSystemFileContents(false, aPath, &length));
146+
if (data) {
147+
mmapped_file->FakeFromData(data, length);
148+
*mmapped_file_out = mmapped_file.release();
149+
return true;
150+
}
151+
return false;
152+
}
153+
#else
140154
bool MapV8File(base::File file,
141155
base::MemoryMappedFile::Region region,
142156
base::MemoryMappedFile** mmapped_file_out) {
@@ -203,6 +217,7 @@ base::File OpenV8File(const char* file_name,
203217
OpenV8FileResult::MAX_VALUE);
204218
return file;
205219
}
220+
#endif
206221

207222
#endif // defined(V8_USE_EXTERNAL_STARTUP_DATA)
208223

@@ -504,12 +519,23 @@ void V8Initializer::LoadV8Snapshot(V8SnapshotFileType snapshot_file_type) {
504519
return;
505520
}
506521

522+
#if BUILDFLAG(IS_MAC)
523+
if (g_mapped_snapshot) {
524+
return;
525+
}
526+
g_snapshot_file_type = snapshot_file_type;
527+
if (!FakeMapV8File(GetSnapshotFileName(snapshot_file_type), &g_mapped_snapshot)) {
528+
LOG(FATAL) << "Error loading V8 startup snapshot file";
529+
}
530+
#else
507531
base::MemoryMappedFile::Region file_region;
508532
base::File file =
509533
OpenV8File(GetSnapshotFileName(snapshot_file_type), &file_region);
510534
LoadV8SnapshotFromFile(std::move(file), &file_region, snapshot_file_type);
535+
#endif
511536
}
512537

538+
#if !BUILDFLAG(IS_MAC)
513539
// static
514540
void V8Initializer::LoadV8SnapshotFromFile(
515541
base::File snapshot_file,
@@ -537,6 +563,7 @@ void V8Initializer::LoadV8SnapshotFromFile(
537563
return;
538564
}
539565
}
566+
#endif
540567

541568
#if BUILDFLAG(IS_ANDROID)
542569
// static

0 commit comments

Comments
 (0)