forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegress141275.mm
More file actions
386 lines (316 loc) · 12.9 KB
/
Regress141275.mm
File metadata and controls
386 lines (316 loc) · 12.9 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* Copyright (C) 2015 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. AND ITS CONTRIBUTORS ``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 ITS 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 "Regress141275.h"
#import <Foundation/Foundation.h>
#import <objc/objc.h>
#import <objc/runtime.h>
#if JSC_OBJC_API_ENABLED
extern "C" void JSSynchronousGarbageCollectForDebugging(JSContextRef);
extern int failed;
static const NSUInteger scriptToEvaluate = 50;
@interface JSTEvaluator : NSObject
- (instancetype)initWithScript:(NSString*)script;
- (void)insertSignPostWithCompletion:(void(^)(NSError* error))completionHandler;
- (void)evaluateScript:(NSString*)script completion:(void(^)(NSError* error))completionHandler;
- (void)evaluateBlock:(void(^)(JSContext* context))evaluationBlock completion:(void(^)(NSError* error))completionHandler;
- (void)waitForTasksDoneAndReportResults;
@end
static const NSString* JSTEvaluatorThreadContextKey = @"JSTEvaluatorThreadContextKey";
/*
* A JSTEvaluatorThreadContext is kept in the thread dictionary of threads used by JSEvaluator.
*
* This includes the run loop thread, and any threads used by _jsSourcePerformQueue to execute a task.
*/
@interface JSTEvaluatorThreadContext : NSObject
@property (weak) JSTEvaluator* evaluator;
@property (strong) JSContext* jsContext;
@end
@implementation JSTEvaluatorThreadContext
@end
/*!
* A JSTEvaluatorTask is a single task to be executed.
*
* JSTEvaluator keeps a list of pending tasks. The run loop thread is repsonsible for feeding pending tasks to the _jsSourcePerformQueue, while respecting sign posts.
*/
@interface JSTEvaluatorTask : NSObject
@property (nonatomic, copy) void (^evaluateBlock)(JSContext* jsContext);
@property (nonatomic, copy) void (^completionHandler)(NSError* error);
@property (nonatomic, copy) NSError* error;
+ (instancetype)evaluatorTaskWithEvaluateBlock:(void (^)(JSContext*))block completionHandler:(void (^)(NSError* error))completionBlock;
@end
@implementation JSTEvaluatorTask
+ (instancetype)evaluatorTaskWithEvaluateBlock:(void (^)(JSContext*))evaluationBlock completionHandler:(void (^)(NSError* error))completionHandler
{
JSTEvaluatorTask* task = [self new];
task.evaluateBlock = evaluationBlock;
task.completionHandler = completionHandler;
return task;
}
@end
@implementation JSTEvaluator {
dispatch_queue_t _jsSourcePerformQueue;
dispatch_semaphore_t _allScriptsDone;
CFRunLoopRef _jsThreadRunLoop;
CFRunLoopSourceRef _jsThreadRunLoopSource;
JSContext* _jsContext;
NSMutableArray* __pendingTasks;
}
- (instancetype)init
{
self = [super init];
if (self) {
_jsSourcePerformQueue = dispatch_queue_create("JSTEval", DISPATCH_QUEUE_CONCURRENT);
_allScriptsDone = dispatch_semaphore_create(0);
_jsContext = [JSContext new];
_jsContext.name = @"JSTEval";
__pendingTasks = [NSMutableArray new];
NSThread* jsThread = [[NSThread alloc] initWithTarget:self selector:@selector(_jsThreadMain) object:nil];
[jsThread setName:@"JSTEval"];
[jsThread start];
}
return self;
}
- (instancetype)initWithScript:(NSString*)script
{
self = [self init];
if (self) {
dispatch_semaphore_t dsema = dispatch_semaphore_create(0);
[self evaluateScript:script
completion:^(NSError*) {
dispatch_semaphore_signal(dsema);
}];
dispatch_semaphore_wait(dsema, DISPATCH_TIME_FOREVER);
}
return self;
}
- (void)_accessPendingTasksWithBlock:(void(^)(NSMutableArray* pendingTasks))block
{
@synchronized(self) {
block(__pendingTasks);
if (__pendingTasks.count > 0) {
if (_jsThreadRunLoop && _jsThreadRunLoopSource) {
CFRunLoopSourceSignal(_jsThreadRunLoopSource);
CFRunLoopWakeUp(_jsThreadRunLoop);
}
}
}
}
- (void)insertSignPostWithCompletion:(void(^)(NSError* error))completionHandler
{
[self _accessPendingTasksWithBlock:^(NSMutableArray* pendingTasks) {
JSTEvaluatorTask* task = [JSTEvaluatorTask evaluatorTaskWithEvaluateBlock:nil
completionHandler:completionHandler];
[pendingTasks addObject:task];
}];
}
- (void)evaluateScript:(NSString*)script completion:(void(^)(NSError* error))completionHandler
{
[self evaluateBlock:^(JSContext* context) {
[context evaluateScript:script];
} completion:completionHandler];
}
- (void)evaluateBlock:(void(^)(JSContext* context))evaluationBlock completion:(void(^)(NSError* error))completionHandler
{
NSParameterAssert(evaluationBlock != nil);
[self _accessPendingTasksWithBlock:^(NSMutableArray* pendingTasks) {
JSTEvaluatorTask* task = [JSTEvaluatorTask evaluatorTaskWithEvaluateBlock:evaluationBlock
completionHandler:completionHandler];
[pendingTasks addObject:task];
}];
}
- (void)waitForTasksDoneAndReportResults
{
NSString* passFailString = @"PASSED";
if (!dispatch_semaphore_wait(_allScriptsDone, dispatch_time(DISPATCH_TIME_NOW, 30 * NSEC_PER_SEC))) {
int totalScriptsRun = [_jsContext[@"counter"] toInt32];
if (totalScriptsRun != scriptToEvaluate) {
passFailString = @"FAILED";
failed = 1;
}
NSLog(@" Ran a total of %d scripts: %@", totalScriptsRun, passFailString);
} else {
passFailString = @"FAILED";
failed = 1;
NSLog(@" Error, timeout waiting for all tasks to complete: %@", passFailString);
}
}
static void __JSTRunLoopSourceScheduleCallBack(void* info, CFRunLoopRef rl, CFStringRef)
{
@autoreleasepool {
[(__bridge JSTEvaluator*)info _sourceScheduledOnRunLoop:rl];
}
}
static void __JSTRunLoopSourcePerformCallBack(void* info )
{
@autoreleasepool {
[(__bridge JSTEvaluator*)info _sourcePerform];
}
}
static void __JSTRunLoopSourceCancelCallBack(void* info, CFRunLoopRef rl, CFStringRef)
{
@autoreleasepool {
[(__bridge JSTEvaluator*)info _sourceCanceledOnRunLoop:rl];
}
}
- (void)_jsThreadMain
{
@autoreleasepool {
const CFIndex kRunLoopSourceContextVersion = 0;
CFRunLoopSourceContext sourceContext = {
kRunLoopSourceContextVersion, (__bridge void*)(self),
NULL, NULL, NULL, NULL, NULL,
__JSTRunLoopSourceScheduleCallBack,
__JSTRunLoopSourceCancelCallBack,
__JSTRunLoopSourcePerformCallBack
};
@synchronized(self) {
_jsThreadRunLoop = CFRunLoopGetCurrent();
CFRetain(_jsThreadRunLoop);
_jsThreadRunLoopSource = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &sourceContext);
CFRunLoopAddSource(_jsThreadRunLoop, _jsThreadRunLoopSource, kCFRunLoopDefaultMode);
}
CFRunLoopRun();
@synchronized(self) {
NSMutableDictionary* threadDict = [[NSThread currentThread] threadDictionary];
[threadDict removeObjectForKey:threadDict[JSTEvaluatorThreadContextKey]];
CFRelease(_jsThreadRunLoopSource);
_jsThreadRunLoopSource = NULL;
CFRelease(_jsThreadRunLoop);
_jsThreadRunLoop = NULL;
__pendingTasks = nil;
}
}
}
- (void)_sourceScheduledOnRunLoop:(CFRunLoopRef)runLoop
{
UNUSED_PARAM(runLoop);
assert([[[NSThread currentThread] name] isEqualToString:@"JSTEval"]);
// Wake up the run loop in case requests were submitted prior to the
// run loop & run loop source getting created.
CFRunLoopSourceSignal(_jsThreadRunLoopSource);
CFRunLoopWakeUp(_jsThreadRunLoop);
}
- (void)_setupEvaluatorThreadContextIfNeeded
{
NSMutableDictionary* threadDict = [[NSThread currentThread] threadDictionary];
JSTEvaluatorThreadContext* context = threadDict[JSTEvaluatorThreadContextKey];
// The evaluator may be other evualuator, or nil if this thread has not been used before. Eaither way take ownership.
if (context.evaluator != self) {
context = [JSTEvaluatorThreadContext new];
context.evaluator = self;
threadDict[JSTEvaluatorThreadContextKey] = context;
}
}
- (void)_callCompletionHandler:(void(^)(NSError* error))completionHandler ifNeededWithError:(NSError*)error
{
if (completionHandler) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
completionHandler(error);
});
}
}
- (void)_sourcePerform
{
assert([[[NSThread currentThread] name] isEqualToString:@"JSTEval"]);
__block NSArray* tasks = nil;
[self _accessPendingTasksWithBlock:^(NSMutableArray* pendingTasks) {
// No signpost, take all tasks.
tasks = [pendingTasks copy];
[pendingTasks removeAllObjects];
}];
if (tasks.count > 0) {
for (JSTEvaluatorTask* task in tasks) {
dispatch_block_t block = ^{
NSError* error = nil;
if (task.evaluateBlock) {
[self _setupEvaluatorThreadContextIfNeeded];
task.evaluateBlock(self->_jsContext);
if (self->_jsContext.exception) {
NSLog(@"Did fail on JSContext: %@", self->_jsContext.name);
NSDictionary* userInfo = @{ NSLocalizedDescriptionKey : [self->_jsContext.exception[@"message"] toString] };
error = [NSError errorWithDomain:@"JSTEvaluator" code:1 userInfo:userInfo];
self->_jsContext.exception = nil;
}
}
[self _callCompletionHandler:task.completionHandler ifNeededWithError:error];
};
if (task.evaluateBlock)
dispatch_async(_jsSourcePerformQueue, block);
else
dispatch_barrier_async(_jsSourcePerformQueue, block);
}
dispatch_barrier_sync(_jsSourcePerformQueue, ^{
if ([self->_jsContext[@"counter"] toInt32] == scriptToEvaluate)
dispatch_semaphore_signal(self->_allScriptsDone);
});
}
}
- (void)_sourceCanceledOnRunLoop:(CFRunLoopRef)runLoop
{
UNUSED_PARAM(runLoop);
assert([[[NSThread currentThread] name] isEqualToString:@"JSTEval"]);
@synchronized(self) {
assert(_jsThreadRunLoop);
assert(_jsThreadRunLoopSource);
CFRunLoopRemoveSource(_jsThreadRunLoop, _jsThreadRunLoopSource, kCFRunLoopDefaultMode);
CFRunLoopStop(_jsThreadRunLoop);
}
}
@end
void runRegress141275()
{
// Test that we can execute the same script from multiple threads with a shared context.
// See <https://webkit.org/b/141275>
NSLog(@"TEST: Testing multiple threads executing the same script with a shared context");
@autoreleasepool {
JSTEvaluator* evaluator = [[JSTEvaluator alloc] initWithScript:@"this['counter'] = 0;"];
void (^showErrorIfNeeded)(NSError* error) = ^(NSError* error) {
if (error) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Error: %@", error);
});
}
};
[evaluator evaluateBlock:^(JSContext* context) {
JSSynchronousGarbageCollectForDebugging([context JSGlobalContextRef]);
} completion:showErrorIfNeeded];
[evaluator evaluateBlock:^(JSContext* context) {
context[@"wait"] = ^{
[NSThread sleepForTimeInterval:0.01];
};
} completion:^(NSError* error) {
if (error) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Error: %@", error);
});
}
for (unsigned i = 0; i < scriptToEvaluate; i++)
[evaluator evaluateScript:@"this['counter']++; this['wait']();" completion:showErrorIfNeeded];
}];
[evaluator waitForTasksDoneAndReportResults];
}
}
#endif // JSC_OBJC_API_ENABLED