-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
JSScript.mm
360 lines (299 loc) · 12.1 KB
/
JSScript.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Copyright (C) 2019-2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "config.h"
#import "JSBase.h"
#import "JSScriptInternal.h"
#import "APICast.h"
#import "BytecodeCacheError.h"
#import "CachedTypes.h"
#import "CodeCache.h"
#import "Completion.h"
#import "Identifier.h"
#import "IntegrityInlines.h"
#import "JSContextInternal.h"
#import "JSScriptSourceProvider.h"
#import "JSSourceCode.h"
#import "JSValuePrivate.h"
#import "JSVirtualMachineInternal.h"
#import "Symbol.h"
#import <sys/stat.h>
#import <wtf/FileSystem.h>
#import <wtf/SHA1.h>
#import <wtf/SafeStrerror.h>
#import <wtf/Scope.h>
#import <wtf/WeakObjCPtr.h>
#import <wtf/spi/darwin/DataVaultSPI.h>
#if JSC_OBJC_API_ENABLED
@implementation JSScript {
WeakObjCPtr<JSVirtualMachine> m_virtualMachine;
JSScriptType m_type;
FileSystem::MappedFileData m_mappedSource;
String m_source;
RetainPtr<NSURL> m_sourceURL;
RetainPtr<NSURL> m_cachePath;
RefPtr<JSC::CachedBytecode> m_cachedBytecode;
}
static JSScript *createError(NSString *message, NSError** error)
{
if (error)
*error = [NSError errorWithDomain:@"JSScriptErrorDomain" code:1 userInfo:@{ @"message": message }];
return nil;
}
static bool validateBytecodeCachePath(NSURL* cachePath, NSError** error)
{
if (!cachePath)
return true;
URL cachePathURL(cachePath.absoluteURL);
if (!cachePathURL.isLocalFile()) {
createError([NSString stringWithFormat:@"Cache path `%@` is not a local file", static_cast<NSURL *>(cachePathURL)], error);
return false;
}
String systemPath = cachePathURL.fileSystemPath();
if (auto fileType = FileSystem::fileType(systemPath)) {
if (*fileType != FileSystem::FileType::Regular) {
createError([NSString stringWithFormat:@"Cache path `%@` already exists and is not a file", static_cast<NSString *>(systemPath)], error);
return false;
}
}
String directory = FileSystem::parentPath(systemPath);
if (directory.isNull()) {
createError([NSString stringWithFormat:@"Cache path `%@` does not contain in a valid directory", static_cast<NSString *>(systemPath)], error);
return false;
}
if (FileSystem::fileType(directory) != FileSystem::FileType::Directory) {
createError([NSString stringWithFormat:@"Cache directory `%@` is not a directory or does not exist", static_cast<NSString *>(directory)], error);
return false;
}
#if USE(APPLE_INTERNAL_SDK)
if (rootless_check_datavault_flag(FileSystem::fileSystemRepresentation(directory).data(), nullptr)) {
createError([NSString stringWithFormat:@"Cache directory `%@` is not a data vault", static_cast<NSString *>(directory)], error);
return false;
}
#endif
return true;
}
+ (instancetype)scriptOfType:(JSScriptType)type withSource:(NSString *)source andSourceURL:(NSURL *)sourceURL andBytecodeCache:(NSURL *)cachePath inVirtualMachine:(JSVirtualMachine *)vm error:(out NSError **)error
{
if (!validateBytecodeCachePath(cachePath, error))
return nil;
auto result = adoptNS([[JSScript alloc] init]);
result->m_virtualMachine = vm;
result->m_type = type;
result->m_source = source;
result->m_sourceURL = sourceURL;
result->m_cachePath = cachePath;
[result readCache];
return result.autorelease();
}
+ (instancetype)scriptOfType:(JSScriptType)type memoryMappedFromASCIIFile:(NSURL *)filePath withSourceURL:(NSURL *)sourceURL andBytecodeCache:(NSURL *)cachePath inVirtualMachine:(JSVirtualMachine *)vm error:(out NSError **)error
{
if (!validateBytecodeCachePath(cachePath, error))
return nil;
URL filePathURL(filePath.absoluteURL);
if (!filePathURL.isLocalFile())
return createError([NSString stringWithFormat:@"File path %@ is not a local file", static_cast<NSURL *>(filePathURL)], error);
bool success = false;
String systemPath = filePathURL.fileSystemPath();
FileSystem::MappedFileData fileData(systemPath, FileSystem::MappedFileMode::Shared, success);
if (!success)
return createError([NSString stringWithFormat:@"File at path %@ could not be mapped.", static_cast<NSString *>(systemPath)], error);
if (!charactersAreAllASCII(reinterpret_cast<const LChar*>(fileData.data()), fileData.size()))
return createError([NSString stringWithFormat:@"Not all characters in file at %@ are ASCII.", static_cast<NSString *>(systemPath)], error);
auto result = adoptNS([[JSScript alloc] init]);
result->m_virtualMachine = vm;
result->m_type = type;
result->m_source = String(StringImpl::createWithoutCopying(bitwise_cast<const LChar*>(fileData.data()), fileData.size()));
result->m_mappedSource = WTFMove(fileData);
result->m_sourceURL = sourceURL;
result->m_cachePath = cachePath;
[result readCache];
return result.autorelease();
}
- (void)readCache
{
if (!m_cachePath)
return;
String cacheFilename = m_cachePath.get().path;
auto fd = FileSystem::openAndLockFile(cacheFilename, FileSystem::FileOpenMode::Read, {FileSystem::FileLockMode::Exclusive, FileSystem::FileLockMode::Nonblocking});
if (!FileSystem::isHandleValid(fd))
return;
auto closeFD = makeScopeExit([&] {
FileSystem::unlockAndCloseFile(fd);
});
bool success;
FileSystem::MappedFileData mappedFile(fd, FileSystem::MappedFileMode::Private, success);
if (!success)
return;
const uint8_t* fileData = reinterpret_cast<const uint8_t*>(mappedFile.data());
size_t fileTotalSize = mappedFile.size();
// Ensure we at least have a SHA1::Digest to read.
if (fileTotalSize < sizeof(SHA1::Digest)) {
FileSystem::deleteFile(cacheFilename);
return;
}
size_t fileDataSize = fileTotalSize - sizeof(SHA1::Digest);
SHA1::Digest computedHash;
SHA1 sha1;
sha1.addBytes(fileData, fileDataSize);
sha1.computeHash(computedHash);
SHA1::Digest fileHash;
memcpy(&fileHash, fileData + fileDataSize, sizeof(SHA1::Digest));
if (computedHash != fileHash) {
FileSystem::deleteFile(cacheFilename);
return;
}
Ref<JSC::CachedBytecode> cachedBytecode = JSC::CachedBytecode::create(WTFMove(mappedFile));
JSC::VM& vm = *toJS([m_virtualMachine JSContextGroupRef]);
JSC::SourceCode sourceCode = self.sourceCode;
JSC::SourceCodeKey key = m_type == kJSScriptTypeProgram ? sourceCodeKeyForSerializedProgram(vm, sourceCode) : sourceCodeKeyForSerializedModule(vm, sourceCode);
if (isCachedBytecodeStillValid(vm, cachedBytecode.copyRef(), key, m_type == kJSScriptTypeProgram ? JSC::SourceCodeType::ProgramType : JSC::SourceCodeType::ModuleType))
m_cachedBytecode = WTFMove(cachedBytecode);
else
FileSystem::truncateFile(fd, 0);
}
- (BOOL)cacheBytecodeWithError:(NSError **)error
{
String errorString { };
[self writeCache:errorString];
if (!errorString.isNull()) {
createError(errorString, error);
return NO;
}
return YES;
}
- (BOOL)isUsingBytecodeCache
{
return !!m_cachedBytecode->size();
}
- (NSURL *)sourceURL
{
return m_sourceURL.get();
}
- (JSScriptType)type
{
return m_type;
}
@end
@implementation JSScript(Internal)
- (instancetype)init
{
self = [super init];
if (!self)
return nil;
self->m_cachedBytecode = JSC::CachedBytecode::create();
return self;
}
- (unsigned)hash
{
return m_source.hash();
}
- (const String&)source
{
return m_source;
}
- (RefPtr<JSC::CachedBytecode>)cachedBytecode
{
return m_cachedBytecode;
}
- (JSC::SourceCode)sourceCode
{
JSC::VM& vm = *toJS([m_virtualMachine JSContextGroupRef]);
JSC::JSLockHolder locker(vm);
TextPosition startPosition { };
String filename = String { self.sourceURL.absoluteString };
URL url = URL({ }, filename);
auto type = m_type == kJSScriptTypeModule ? JSC::SourceProviderSourceType::Module : JSC::SourceProviderSourceType::Program;
JSC::SourceOrigin origin(url);
Ref<JSScriptSourceProvider> sourceProvider = JSScriptSourceProvider::create(self, origin, WTFMove(filename), startPosition, type);
JSC::SourceCode sourceCode(WTFMove(sourceProvider), startPosition.m_line.oneBasedInt(), startPosition.m_column.oneBasedInt());
return sourceCode;
}
- (JSC::JSSourceCode*)jsSourceCode
{
JSC::VM& vm = *toJS([m_virtualMachine JSContextGroupRef]);
JSC::JSLockHolder locker(vm);
JSC::JSSourceCode* jsSourceCode = JSC::JSSourceCode::create(vm, self.sourceCode);
return jsSourceCode;
}
- (BOOL)writeCache:(String&)error
{
if (self.isUsingBytecodeCache) {
error = "Cache for JSScript is already non-empty. Can not override it."_s;
return NO;
}
if (!m_cachePath) {
error = "No cache path was provided during construction of this JSScript."_s;
return NO;
}
// We want to do the write as a transaction (i.e. we guarantee that it's all
// or nothing). So, we'll write to a temp file first, and rename the temp
// file to the cache file only after we've finished writing the whole thing.
NSString *cachePathString = m_cachePath.get().path;
const char* cacheFileName = cachePathString.UTF8String;
const char* tempFileName = [cachePathString stringByAppendingString:@".tmp"].UTF8String;
int fd = open(cacheFileName, O_CREAT | O_WRONLY | O_EXLOCK | O_NONBLOCK, 0600);
if (fd == -1) {
error = makeString("Could not open or lock the bytecode cache file. It's likely another VM or process is already using it. Error: ", safeStrerror(errno).data());
return NO;
}
auto closeFD = makeScopeExit([&] {
close(fd);
});
int tempFD = open(tempFileName, O_CREAT | O_RDWR | O_EXLOCK | O_NONBLOCK, 0600);
if (tempFD == -1) {
error = makeString("Could not open or lock the bytecode cache temp file. Error: ", safeStrerror(errno).data());
return NO;
}
auto closeTempFD = makeScopeExit([&] {
close(tempFD);
});
JSC::BytecodeCacheError cacheError;
JSC::SourceCode sourceCode = self.sourceCode;
JSC::VM& vm = *toJS([m_virtualMachine JSContextGroupRef]);
switch (m_type) {
case kJSScriptTypeModule:
m_cachedBytecode = JSC::generateModuleBytecode(vm, sourceCode, tempFD, cacheError);
break;
case kJSScriptTypeProgram:
m_cachedBytecode = JSC::generateProgramBytecode(vm, sourceCode, tempFD, cacheError);
break;
}
if (cacheError.isValid()) {
m_cachedBytecode = JSC::CachedBytecode::create();
FileSystem::truncateFile(fd, 0);
error = makeString("Unable to generate bytecode for this JSScript because: ", cacheError.message());
return NO;
}
SHA1::Digest computedHash;
SHA1 sha1;
sha1.addBytes(m_cachedBytecode->data(), m_cachedBytecode->size());
sha1.computeHash(computedHash);
FileSystem::writeToFile(tempFD, computedHash.data(), sizeof(computedHash));
fsync(tempFD);
rename(tempFileName, cacheFileName);
return YES;
}
@end
#endif