Skip to content

Commit a9cbc97

Browse files
committed
Bytecode cache should use FileSystem
https://bugs.webkit.org/show_bug.cgi?id=199759 Reviewed by Yusuke Suzuki. Source/JavaScriptCore: Update bytecode cache to use platform generic FileSystem calls. * API/JSScript.mm: * CMakeLists.txt: * jsc.cpp: * runtime/CachePayload.cpp: * runtime/CachePayload.h: * runtime/CachedBytecode.h: * runtime/CachedTypes.cpp: * runtime/CachedTypes.h: * runtime/CodeCache.cpp: * runtime/CodeCache.h: * runtime/Completion.cpp: * runtime/Completion.h: Source/WebCore: * platform/SharedBuffer.cpp: Source/WTF: * wtf/FileSystem.cpp: * wtf/FileSystem.h: Add support for creating MappedFileData from a preexisting file handle for use with locked files. Also support creating MappedFileData with either private or shared mappings. * wtf/UUID.cpp: Avoid ASSERT with returning a LazyNeverDestroyed object that hasn't been created. * wtf/glib/FileSystemGlib.cpp: * wtf/win/FileSystemWin.cpp: Add truncateFile implementations. Tools: * TestWebKitAPI/Tests/WTF/FileSystem.cpp: Canonical link: https://commits.webkit.org/213727@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@247505 268f45cc-cd09-0410-ab3c-d52691b4dbfc
1 parent bd48239 commit a9cbc97

23 files changed

Lines changed: 216 additions & 131 deletions

‎Source/JavaScriptCore/API/JSScript.mm‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ + (instancetype)scriptOfType:(JSScriptType)type memoryMappedFromASCIIFile:(NSURL
130130

131131
bool success = false;
132132
String systemPath = filePathURL.fileSystemPath();
133-
FileSystem::MappedFileData fileData(systemPath, success);
133+
FileSystem::MappedFileData fileData(systemPath, FileSystem::MappedFileMode::Shared, success);
134134
if (!success)
135135
return createError([NSString stringWithFormat:@"File at path %@ could not be mapped.", static_cast<NSString *>(systemPath)], error);
136136

@@ -153,22 +153,19 @@ - (void)readCache
153153
if (!m_cachePath)
154154
return;
155155

156-
int fd = open([m_cachePath path].UTF8String, O_RDONLY | O_EXLOCK | O_NONBLOCK, 0666);
157-
if (fd == -1)
156+
auto fd = FileSystem::openAndLockFile([m_cachePath path].UTF8String, FileSystem::FileOpenMode::Read, {FileSystem::FileLockMode::Exclusive, FileSystem::FileLockMode::Nonblocking});
157+
if (!FileSystem::isHandleValid(fd))
158158
return;
159159
auto closeFD = makeScopeExit([&] {
160-
close(fd);
160+
FileSystem::unlockAndCloseFile(fd);
161161
});
162162

163-
struct stat sb;
164-
int res = fstat(fd, &sb);
165-
size_t size = static_cast<size_t>(sb.st_size);
166-
if (res || !size)
163+
bool success;
164+
FileSystem::MappedFileData mappedFile(fd, FileSystem::MappedFileMode::Private, success);
165+
if (!success)
167166
return;
168167

169-
void* buffer = mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0);
170-
171-
Ref<JSC::CachedBytecode> cachedBytecode = JSC::CachedBytecode::create(buffer, size);
168+
Ref<JSC::CachedBytecode> cachedBytecode = JSC::CachedBytecode::create(WTFMove(mappedFile));
172169

173170
JSC::VM& vm = [m_virtualMachine vm];
174171
JSC::SourceCode sourceCode = [self sourceCode];
@@ -291,7 +288,7 @@ - (BOOL)writeCache:(String&)error
291288

292289
if (cacheError.isValid()) {
293290
m_cachedBytecode = JSC::CachedBytecode::create();
294-
ftruncate(fd, 0);
291+
FileSystem::truncateFile(fd, 0);
295292
error = makeString("Unable to generate bytecode for this JSScript because: ", cacheError.message());
296293
return NO;
297294
}

‎Source/JavaScriptCore/CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ add_custom_command(
223223
if (WTF_OS_MAC_OS_X)
224224
execute_process(COMMAND bash -c "date +'%s'" OUTPUT_VARIABLE BUILD_TIME OUTPUT_STRIP_TRAILING_WHITESPACE)
225225
else ()
226-
set(BUILD_TIME 0)
226+
string(TIMESTAMP BUILD_TIME "%s")
227227
endif ()
228228

229229
file(WRITE ${JavaScriptCore_DERIVED_SOURCES_DIR}/BytecodeCacheVersion.h "#define JSC_BYTECODE_CACHE_VERSION ${BUILD_TIME}\n")

‎Source/JavaScriptCore/ChangeLog‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
2019-07-16 Christopher Reid <chris.reid@sony.com>
2+
3+
Bytecode cache should use FileSystem
4+
https://bugs.webkit.org/show_bug.cgi?id=199759
5+
6+
Reviewed by Yusuke Suzuki.
7+
8+
Update bytecode cache to use platform generic FileSystem calls.
9+
10+
* API/JSScript.mm:
11+
* CMakeLists.txt:
12+
* jsc.cpp:
13+
* runtime/CachePayload.cpp:
14+
* runtime/CachePayload.h:
15+
* runtime/CachedBytecode.h:
16+
* runtime/CachedTypes.cpp:
17+
* runtime/CachedTypes.h:
18+
* runtime/CodeCache.cpp:
19+
* runtime/CodeCache.h:
20+
* runtime/Completion.cpp:
21+
* runtime/Completion.h:
22+
123
2019-07-16 Joonghun Park <pjh0718@gmail.com>
224

325
[GTK] Fix a build warning in JavaScriptCore/API/tests/testapi.c

‎Source/JavaScriptCore/jsc.cpp‎

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
#include <type_traits>
8686
#include <wtf/Box.h>
8787
#include <wtf/CommaPrinter.h>
88+
#include <wtf/FileSystem.h>
8889
#include <wtf/MainThread.h>
8990
#include <wtf/MemoryPressureHandler.h>
9091
#include <wtf/MonotonicTime.h>
@@ -1003,7 +1004,6 @@ class ShellSourceProvider : public StringSourceProvider {
10031004

10041005
void commitCachedBytecode() const override
10051006
{
1006-
#if OS(DARWIN)
10071007
if (!cacheEnabled() || !m_cachedBytecode || !m_cachedBytecode->hasUpdates())
10081008
return;
10091009

@@ -1012,32 +1012,33 @@ class ShellSourceProvider : public StringSourceProvider {
10121012
});
10131013

10141014
String filename = cachePath();
1015-
int fd = open(filename.utf8().data(), O_CREAT | O_WRONLY | O_TRUNC | O_EXLOCK | O_NONBLOCK, 0666);
1016-
if (fd == -1)
1015+
auto fd = FileSystem::openAndLockFile(filename, FileSystem::FileOpenMode::Write, {FileSystem::FileLockMode::Exclusive, FileSystem::FileLockMode::Nonblocking});
1016+
if (!FileSystem::isHandleValid(fd))
10171017
return;
10181018

10191019
auto closeFD = makeScopeExit([&] {
1020-
close(fd);
1020+
FileSystem::unlockAndCloseFile(fd);
10211021
});
10221022

1023-
struct stat sb;
1024-
int res = fstat(fd, &sb);
1025-
size_t size = static_cast<size_t>(sb.st_size);
1026-
if (res || size != m_cachedBytecode->size()) {
1023+
long long fileSize;
1024+
if (!FileSystem::getFileSize(fd, fileSize))
1025+
return;
1026+
1027+
size_t cacheFileSize;
1028+
if (!WTF::convertSafely(fileSize, cacheFileSize) || cacheFileSize != m_cachedBytecode->size()) {
10271029
// The bytecode cache has already been updated
10281030
return;
10291031
}
10301032

1031-
if (ftruncate(fd, m_cachedBytecode->sizeForUpdate()))
1033+
if (!FileSystem::truncateFile(fd, m_cachedBytecode->sizeForUpdate()))
10321034
return;
10331035

10341036
m_cachedBytecode->commitUpdates([&] (off_t offset, const void* data, size_t size) {
1035-
off_t result = lseek(fd, offset, SEEK_SET);
1037+
long long result = FileSystem::seekFile(fd, offset, FileSystem::FileSeekOrigin::Beginning);
10361038
ASSERT_UNUSED(result, result != -1);
1037-
size_t bytesWritten = static_cast<size_t>(write(fd, data, size));
1039+
size_t bytesWritten = static_cast<size_t>(FileSystem::writeToFile(fd, static_cast<const char*>(data), size));
10381040
ASSERT_UNUSED(bytesWritten, bytesWritten == size);
10391041
});
1040-
#endif
10411042
}
10421043

10431044
private:
@@ -1046,40 +1047,34 @@ class ShellSourceProvider : public StringSourceProvider {
10461047
if (!cacheEnabled())
10471048
return static_cast<const char*>(nullptr);
10481049
const char* cachePath = Options::diskCachePath();
1049-
String filename = sourceOrigin().string();
1050-
filename.replace('/', '_');
1051-
return makeString(cachePath, '/', source().toString().hash(), '-', filename, ".bytecode-cache");
1050+
String filename = FileSystem::encodeForFileName(sourceOrigin().string());
1051+
return FileSystem::pathByAppendingComponent(cachePath, makeString(source().toString().hash(), '-', filename, ".bytecode-cache"));
10521052
}
10531053

10541054
void loadBytecode() const
10551055
{
1056-
#if OS(DARWIN)
10571056
if (!cacheEnabled())
10581057
return;
10591058

10601059
String filename = cachePath();
10611060
if (filename.isNull())
10621061
return;
10631062

1064-
int fd = open(filename.utf8().data(), O_RDONLY | O_SHLOCK | O_NONBLOCK);
1065-
if (fd == -1)
1063+
auto fd = FileSystem::openAndLockFile(filename, FileSystem::FileOpenMode::Read, {FileSystem::FileLockMode::Shared, FileSystem::FileLockMode::Nonblocking});
1064+
if (!FileSystem::isHandleValid(fd))
10661065
return;
10671066

10681067
auto closeFD = makeScopeExit([&] {
1069-
close(fd);
1068+
FileSystem::unlockAndCloseFile(fd);
10701069
});
10711070

1072-
struct stat sb;
1073-
int res = fstat(fd, &sb);
1074-
size_t size = static_cast<size_t>(sb.st_size);
1075-
if (res || !size)
1076-
return;
1071+
bool success;
1072+
FileSystem::MappedFileData mappedFileData(fd, FileSystem::MappedFileMode::Private, success);
10771073

1078-
void* buffer = mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0);
1079-
if (buffer == MAP_FAILED)
1074+
if (!success)
10801075
return;
1081-
m_cachedBytecode = CachedBytecode::create(buffer, size);
1082-
#endif
1076+
1077+
m_cachedBytecode = CachedBytecode::create(WTFMove(mappedFileData));
10831078
}
10841079

10851080
ShellSourceProvider(const String& source, const SourceOrigin& sourceOrigin, URL&& url, const TextPosition& startPosition, SourceProviderSourceType sourceType)

‎Source/JavaScriptCore/runtime/CachePayload.cpp‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@
3232

3333
namespace JSC {
3434

35-
#if !OS(WINDOWS)
36-
CachePayload CachePayload::makeMappedPayload(void* data, size_t size)
35+
CachePayload CachePayload::makeMappedPayload(FileSystem::MappedFileData&& data)
3736
{
38-
return CachePayload(true, data, size);
37+
return CachePayload(true, data.leakHandle(), data.size());
3938
}
40-
#endif
4139

4240
CachePayload CachePayload::makeMallocPayload(MallocPtr<uint8_t>&& data, size_t size)
4341
{
@@ -76,11 +74,7 @@ void CachePayload::freeData()
7674
if (!m_data)
7775
return;
7876
if (m_mapped) {
79-
#if !OS(WINDOWS)
80-
munmap(m_data, m_size);
81-
#else
82-
RELEASE_ASSERT_NOT_REACHED();
83-
#endif
77+
FileSystem::unmapViewOfFile(m_data, m_size);
8478
} else
8579
fastFree(m_data);
8680
}

‎Source/JavaScriptCore/runtime/CachePayload.h‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@
2525

2626
#pragma once
2727

28+
#include <wtf/FileSystem.h>
2829
#include <wtf/MallocPtr.h>
2930

3031
namespace JSC {
3132

3233
class CachePayload {
3334
public:
34-
#if !OS(WINDOWS)
35-
JS_EXPORT_PRIVATE static CachePayload makeMappedPayload(void*, size_t);
36-
#endif
35+
JS_EXPORT_PRIVATE static CachePayload makeMappedPayload(FileSystem::MappedFileData&&);
3736
JS_EXPORT_PRIVATE static CachePayload makeMallocPayload(MallocPtr<uint8_t>&&, size_t);
3837
JS_EXPORT_PRIVATE static CachePayload makeEmptyPayload();
3938

‎Source/JavaScriptCore/runtime/CachedBytecode.h‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,10 @@ class CachedBytecode : public RefCounted<CachedBytecode> {
4646
return adoptRef(*new CachedBytecode(CachePayload::makeEmptyPayload()));
4747
}
4848

49-
#if !OS(WINDOWS)
50-
static Ref<CachedBytecode> create(void* data, size_t size, LeafExecutableMap&& leafExecutables = { })
49+
static Ref<CachedBytecode> create(FileSystem::MappedFileData&& data, LeafExecutableMap&& leafExecutables = { })
5150
{
52-
return adoptRef(*new CachedBytecode(CachePayload::makeMappedPayload(data, size), WTFMove(leafExecutables)));
51+
return adoptRef(*new CachedBytecode(CachePayload::makeMappedPayload(WTFMove(data)), WTFMove(leafExecutables)));
5352
}
54-
#endif
5553

5654
static Ref<CachedBytecode> create(MallocPtr<uint8_t>&& data, size_t size, LeafExecutableMap&& leafExecutables)
5755
{

‎Source/JavaScriptCore/runtime/CachedTypes.cpp‎

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class Encoder {
8888
ptrdiff_t m_offset;
8989
};
9090

91-
Encoder(VM& vm, int fd = -1)
91+
Encoder(VM& vm, FileSystem::PlatformFileHandle fd = FileSystem::invalidPlatformFileHandle)
9292
: m_vm(vm)
9393
, m_fd(fd)
9494
, m_baseOffset(0)
@@ -152,12 +152,8 @@ class Encoder {
152152
return nullptr;
153153
m_currentPage->alignEnd();
154154

155-
if (m_fd != -1) {
156-
#if !OS(WINDOWS)
155+
if (FileSystem::isHandleValid(m_fd)) {
157156
return releaseMapped(error);
158-
#else
159-
RELEASE_ASSERT_NOT_REACHED();
160-
#endif
161157
}
162158

163159
size_t size = m_baseOffset + m_currentPage->size();
@@ -172,17 +168,16 @@ class Encoder {
172168
}
173169

174170
private:
175-
#if !OS(WINDOWS)
176171
RefPtr<CachedBytecode> releaseMapped(BytecodeCacheError& error)
177172
{
178173
size_t size = m_baseOffset + m_currentPage->size();
179-
if (ftruncate(m_fd, size)) {
174+
if (!FileSystem::truncateFile(m_fd, size)) {
180175
error = BytecodeCacheError::StandardError(errno);
181176
return nullptr;
182177
}
183178

184179
for (const auto& page : m_pages) {
185-
ssize_t bytesWritten = write(m_fd, page.buffer(), page.size());
180+
int bytesWritten = FileSystem::writeToFile(m_fd, reinterpret_cast<char*>(page.buffer()), page.size());
186181
if (bytesWritten == -1) {
187182
error = BytecodeCacheError::StandardError(errno);
188183
return nullptr;
@@ -194,15 +189,15 @@ class Encoder {
194189
}
195190
}
196191

197-
void* buffer = mmap(nullptr, size, PROT_READ, MAP_PRIVATE, m_fd, 0);
198-
if (buffer == MAP_FAILED) {
192+
bool success;
193+
FileSystem::MappedFileData mappedFileData(m_fd, FileSystem::MappedFileMode::Private, success);
194+
if (!success) {
199195
error = BytecodeCacheError::StandardError(errno);
200196
return nullptr;
201197
}
202198

203-
return CachedBytecode::create(buffer, size, WTFMove(m_leafExecutables));
199+
return CachedBytecode::create(WTFMove(mappedFileData), WTFMove(m_leafExecutables));
204200
}
205-
#endif
206201

207202
class Page {
208203
public:
@@ -270,7 +265,7 @@ class Encoder {
270265
}
271266

272267
VM& m_vm;
273-
int m_fd;
268+
FileSystem::PlatformFileHandle m_fd;
274269
ptrdiff_t m_baseOffset;
275270
Page* m_currentPage;
276271
Vector<Page> m_pages;
@@ -2376,7 +2371,7 @@ void encodeCodeBlock(Encoder& encoder, const SourceCodeKey& key, const UnlinkedC
23762371
entry->encode(encoder, { key, jsCast<const UnlinkedCodeBlockType*>(codeBlock) });
23772372
}
23782373

2379-
RefPtr<CachedBytecode> encodeCodeBlock(VM& vm, const SourceCodeKey& key, const UnlinkedCodeBlock* codeBlock, int fd, BytecodeCacheError& error)
2374+
RefPtr<CachedBytecode> encodeCodeBlock(VM& vm, const SourceCodeKey& key, const UnlinkedCodeBlock* codeBlock, FileSystem::PlatformFileHandle fd, BytecodeCacheError& error)
23802375
{
23812376
const ClassInfo* classInfo = codeBlock->classInfo(vm);
23822377

@@ -2394,7 +2389,7 @@ RefPtr<CachedBytecode> encodeCodeBlock(VM& vm, const SourceCodeKey& key, const U
23942389
RefPtr<CachedBytecode> encodeCodeBlock(VM& vm, const SourceCodeKey& key, const UnlinkedCodeBlock* codeBlock)
23952390
{
23962391
BytecodeCacheError error;
2397-
return encodeCodeBlock(vm, key, codeBlock, -1, error);
2392+
return encodeCodeBlock(vm, key, codeBlock, FileSystem::invalidPlatformFileHandle, error);
23982393
}
23992394

24002395
RefPtr<CachedBytecode> encodeFunctionCodeBlock(VM& vm, const UnlinkedFunctionCodeBlock* codeBlock, BytecodeCacheError& error)

‎Source/JavaScriptCore/runtime/CachedTypes.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class Decoder : public RefCounted<Decoder> {
109109
};
110110

111111
JS_EXPORT_PRIVATE RefPtr<CachedBytecode> encodeCodeBlock(VM&, const SourceCodeKey&, const UnlinkedCodeBlock*);
112-
JS_EXPORT_PRIVATE RefPtr<CachedBytecode> encodeCodeBlock(VM&, const SourceCodeKey&, const UnlinkedCodeBlock*, int fd, BytecodeCacheError&);
112+
JS_EXPORT_PRIVATE RefPtr<CachedBytecode> encodeCodeBlock(VM&, const SourceCodeKey&, const UnlinkedCodeBlock*, FileSystem::PlatformFileHandle fd, BytecodeCacheError&);
113113

114114
UnlinkedCodeBlock* decodeCodeBlockImpl(VM&, const SourceCodeKey&, Ref<CachedBytecode>);
115115

‎Source/JavaScriptCore/runtime/CodeCache.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ SourceCodeKey sourceCodeKeyForSerializedModule(VM& vm, const SourceCode& sourceC
225225
return sourceCodeKeyForSerializedBytecode(vm, sourceCode, SourceCodeType::ModuleType, strictMode, scriptMode, { });
226226
}
227227

228-
RefPtr<CachedBytecode> serializeBytecode(VM& vm, UnlinkedCodeBlock* codeBlock, const SourceCode& source, SourceCodeType codeType, JSParserStrictMode strictMode, JSParserScriptMode scriptMode, int fd, BytecodeCacheError& error, OptionSet<CodeGenerationMode> codeGenerationMode)
228+
RefPtr<CachedBytecode> serializeBytecode(VM& vm, UnlinkedCodeBlock* codeBlock, const SourceCode& source, SourceCodeType codeType, JSParserStrictMode strictMode, JSParserScriptMode scriptMode, FileSystem::PlatformFileHandle fd, BytecodeCacheError& error, OptionSet<CodeGenerationMode> codeGenerationMode)
229229
{
230230
return encodeCodeBlock(vm,
231231
sourceCodeKeyForSerializedBytecode(vm, source, codeType, strictMode, scriptMode, codeGenerationMode), codeBlock, fd, error);

0 commit comments

Comments
 (0)