40
40
#import " JSWrapperMapTests.h"
41
41
#import " Regress141275.h"
42
42
#import " Regress141809.h"
43
+ #import < wtf/spi/darwin/DataVaultSPI.h>
43
44
44
45
#if __has_include(<libproc.h>)
45
46
#define HAS_LIBPROC 1
@@ -2076,6 +2077,27 @@ static void testImportModuleTwice()
2076
2077
return [tempDirectory URLByAppendingPathComponent: string];
2077
2078
}
2078
2079
2080
+ static NSURL * cacheFileInDataVault (NSString * name)
2081
+ {
2082
+ #if USE(APPLE_INTERNAL_SDK)
2083
+ static NSURL * dataVaultURL;
2084
+ static dispatch_once_t once;
2085
+ dispatch_once (&once, ^{
2086
+ char userDir[PATH_MAX];
2087
+ RELEASE_ASSERT (confstr (_CS_DARWIN_USER_DIR, userDir, sizeof (userDir)));
2088
+
2089
+ NSString *userDirPath = [[NSFileManager defaultManager ] stringWithFileSystemRepresentation: userDir length: strlen (userDir)];
2090
+ dataVaultURL = [NSURL fileURLWithPath: userDirPath isDirectory: YES ];
2091
+ dataVaultURL = [dataVaultURL URLByAppendingPathComponent: @" JavaScriptCore" isDirectory: YES ];
2092
+ rootless_mkdir_datavault (dataVaultURL.path .UTF8String , 0700 , " JavaScriptCore" );
2093
+ });
2094
+
2095
+ return [dataVaultURL URLByAppendingPathComponent: name isDirectory: NO ];
2096
+ #else
2097
+ return tempFile (name);
2098
+ #endif
2099
+ }
2100
+
2079
2101
static void testModuleBytecodeCache ()
2080
2102
{
2081
2103
@autoreleasepool {
@@ -2087,9 +2109,9 @@ static void testModuleBytecodeCache()
2087
2109
NSURL *barPath = tempFile (@" bar.js" );
2088
2110
NSURL *bazPath = tempFile (@" baz.js" );
2089
2111
2090
- NSURL *fooCachePath = tempFile (@" foo.js.cache" );
2091
- NSURL *barCachePath = tempFile (@" bar.js.cache" );
2092
- NSURL *bazCachePath = tempFile (@" baz.js.cache" );
2112
+ NSURL *fooCachePath = cacheFileInDataVault (@" foo.js.cache" );
2113
+ NSURL *barCachePath = cacheFileInDataVault (@" bar.js.cache" );
2114
+ NSURL *bazCachePath = cacheFileInDataVault (@" baz.js.cache" );
2093
2115
2094
2116
NSURL *fooFakePath = [NSURL fileURLWithPath: @" /foo.js" ];
2095
2117
NSURL *barFakePath = [NSURL fileURLWithPath: @" /directory/bar.js" ];
@@ -2110,7 +2132,8 @@ static void testModuleBytecodeCache()
2110
2132
script = [JSScript scriptOfType: kJSScriptTypeModule memoryMappedFromASCIIFile: bazPath withSourceURL: bazFakePath andBytecodeCache: bazCachePath inVirtualMachine: context.virtualMachine error: nil ];
2111
2133
2112
2134
if (script) {
2113
- if (![script cacheBytecodeWithError: nil ])
2135
+ NSError *error = nil ;
2136
+ if (![script cacheBytecodeWithError: &error])
2114
2137
CRASH ();
2115
2138
[resolve callWithArguments: @[script]];
2116
2139
} else
@@ -2142,7 +2165,7 @@ static void testProgramBytecodeCache()
2142
2165
{
2143
2166
@autoreleasepool {
2144
2167
NSString *fooSource = @" function foo() { return 42; }; function bar() { return 40; }; foo() + bar();" ;
2145
- NSURL *fooCachePath = tempFile (@" foo.js.cache" );
2168
+ NSURL *fooCachePath = cacheFileInDataVault (@" foo.js.cache" );
2146
2169
JSContext *context = [[JSContext alloc ] init ];
2147
2170
JSScript *script = [JSScript scriptOfType: kJSScriptTypeProgram withSource: fooSource andSourceURL: [NSURL URLWithString: @" my-path" ] andBytecodeCache: fooCachePath inVirtualMachine: context.virtualMachine error: nil ];
2148
2171
RELEASE_ASSERT (script);
@@ -2166,7 +2189,7 @@ static void testBytecodeCacheWithSyntaxError(JSScriptType type)
2166
2189
{
2167
2190
@autoreleasepool {
2168
2191
NSString *fooSource = @" this is a syntax error" ;
2169
- NSURL *fooCachePath = tempFile (@" foo.js.cache" );
2192
+ NSURL *fooCachePath = cacheFileInDataVault (@" foo.js.cache" );
2170
2193
JSContext *context = [[JSContext alloc ] init ];
2171
2194
JSScript *script = [JSScript scriptOfType: type withSource: fooSource andSourceURL: [NSURL URLWithString: @" my-path" ] andBytecodeCache: fooCachePath inVirtualMachine: context.virtualMachine error: nil ];
2172
2195
RELEASE_ASSERT (script);
@@ -2180,7 +2203,8 @@ static void testBytecodeCacheWithSyntaxError(JSScriptType type)
2180
2203
2181
2204
static void testBytecodeCacheWithSameCacheFileAndDifferentScript (bool forceDiskCache)
2182
2205
{
2183
- NSURL *cachePath = tempFile (@" cachePath.cache" );
2206
+
2207
+ NSURL *cachePath = cacheFileInDataVault (@" cachePath.cache" );
2184
2208
NSURL *sourceURL = [NSURL URLWithString: @" my-path" ];
2185
2209
2186
2210
@autoreleasepool {
@@ -2220,6 +2244,7 @@ static void testBytecodeCacheWithSameCacheFileAndDifferentScript(bool forceDiskC
2220
2244
NSFileManager * fileManager = [NSFileManager defaultManager ];
2221
2245
BOOL removedAll = [fileManager removeItemAtURL: cachePath error: nil ];
2222
2246
checkResult (@" Removed all temp files created" , removedAll);
2247
+
2223
2248
}
2224
2249
2225
2250
static void testProgramJSScriptException ()
@@ -2245,7 +2270,7 @@ static void testProgramJSScriptException()
2245
2270
2246
2271
static void testCacheFileFailsWhenItsAlreadyCached ()
2247
2272
{
2248
- NSURL * cachePath = tempFile (@" foo.program.cache" );
2273
+ NSURL * cachePath = cacheFileInDataVault (@" foo.program.cache" );
2249
2274
NSURL * sourceURL = [NSURL URLWithString: @" my-path" ];
2250
2275
NSString *source = @" function foo() { return 42; } foo();" ;
2251
2276
@@ -2285,7 +2310,7 @@ static void testCanCacheManyFilesWithTheSameVM()
2285
2310
NSMutableArray *scripts = [[NSMutableArray alloc ] init ];
2286
2311
2287
2312
for (unsigned i = 0 ; i < 10000 ; ++i)
2288
- [cachePaths addObject: tempFile ([NSString stringWithFormat: @" cache-%d .cache" , i])];
2313
+ [cachePaths addObject: cacheFileInDataVault ([NSString stringWithFormat: @" cache-%d .cache" , i])];
2289
2314
2290
2315
JSVirtualMachine *vm = [[JSVirtualMachine alloc ] init ];
2291
2316
bool cachedAll = true ;
@@ -2323,7 +2348,7 @@ static void testCanCacheManyFilesWithTheSameVM()
2323
2348
2324
2349
static void testIsUsingBytecodeCacheAccessor ()
2325
2350
{
2326
- NSURL * cachePath = tempFile (@" foo.program.cache" );
2351
+ NSURL * cachePath = cacheFileInDataVault (@" foo.program.cache" );
2327
2352
NSURL * sourceURL = [NSURL URLWithString: @" my-path" ];
2328
2353
NSString *source = @" function foo() { return 1337; } foo();" ;
2329
2354
@@ -2356,6 +2381,44 @@ static void testIsUsingBytecodeCacheAccessor()
2356
2381
checkResult (@" Successfully removed cache file" , removedAll);
2357
2382
}
2358
2383
2384
+ static void testBytecodeCacheValidation ()
2385
+ {
2386
+ @autoreleasepool {
2387
+ JSContext *context = [[JSContext alloc ] init ];
2388
+
2389
+ auto testInvalidCacheURL = [&](NSURL * cacheURL, NSString * expectedErrorMessage)
2390
+ {
2391
+ NSError * error;
2392
+ JSScript *script = [JSScript scriptOfType: kJSScriptTypeProgram withSource: @" " andSourceURL: [NSURL URLWithString: @" my-path" ] andBytecodeCache: cacheURL inVirtualMachine: context.virtualMachine error: &error];
2393
+ RELEASE_ASSERT (!script);
2394
+ RELEASE_ASSERT (error);
2395
+ NSString * testDesciption = [NSString stringWithFormat: @" Cache path validation for `%@ ` fails with message `%@ `" , cacheURL.absoluteString, expectedErrorMessage];
2396
+ checkResult (testDesciption, [error.description containsString: expectedErrorMessage]);
2397
+ };
2398
+
2399
+ testInvalidCacheURL ([NSURL URLWithString: @" " ], @" Cache path `` is not a local file" );
2400
+ testInvalidCacheURL ([NSURL URLWithString: @" file:///" ], @" Cache path `/` already exists and is not a file" );
2401
+ testInvalidCacheURL ([NSURL URLWithString: @" file:///a/b/c/d/e" ], @" Cache directory `/a/b/c/d` is not a directory or does not exist" );
2402
+ testInvalidCacheURL ([NSURL URLWithString: @" file:///private/tmp/file.cache" ], @" Cache directory `/private/tmp` is not a data vault" );
2403
+ }
2404
+
2405
+ #if USE(APPLE_INTERNAL_SDK)
2406
+ @autoreleasepool {
2407
+ JSContext *context = [[JSContext alloc ] init ];
2408
+
2409
+ auto testValidCacheURL = [&](NSURL * cacheURL)
2410
+ {
2411
+ NSError * error;
2412
+ JSScript *script = [JSScript scriptOfType: kJSScriptTypeProgram withSource: @" " andSourceURL: [NSURL URLWithString: @" my-path" ] andBytecodeCache: cacheURL inVirtualMachine: context.virtualMachine error: &error];
2413
+ NSString * testDesciption = [NSString stringWithFormat: @" Cache path validation for `%@ ` passed" , cacheURL.absoluteString];
2414
+ checkResult (testDesciption, script && !error);
2415
+ };
2416
+
2417
+ testValidCacheURL (cacheFileInDataVault (@" file.cache" ));
2418
+ }
2419
+ #endif
2420
+ }
2421
+
2359
2422
@interface JSContextFileLoaderDelegate : JSContext <JSModuleLoaderDelegate>
2360
2423
2361
2424
+ (instancetype )newContext ;
@@ -2628,6 +2691,7 @@ void testObjectiveCAPI(const char* filter)
2628
2691
RUN (testCacheFileFailsWhenItsAlreadyCached ());
2629
2692
RUN (testCanCacheManyFilesWithTheSameVM ());
2630
2693
RUN (testIsUsingBytecodeCacheAccessor ());
2694
+ RUN (testBytecodeCacheValidation ());
2631
2695
2632
2696
RUN (testLoaderRejectsNilScriptURL ());
2633
2697
RUN (testLoaderRejectsFailedFetch ());
0 commit comments