@@ -323,7 +323,7 @@ bool _task_serialExecutor_isSameExclusiveExecutionContext(
323
323
enum IsCurrentExecutorCheckMode: unsigned {
324
324
// / The default mode when an app was compiled against "new" enough SDK.
325
325
// / It allows crashing in isCurrentExecutor, and calls into `checkIsolated`.
326
- Default_UseCheckIsolated_AllowCrash ,
326
+ Swift6_UseCheckIsolated_AllowCrash ,
327
327
// / Legacy mode; Primarily to support old applications which used data race
328
328
// / detector with "warning" mode, which is no longer supported. When such app
329
329
// / is re-compiled against a new SDK, it will see crashes in what was
@@ -332,17 +332,18 @@ enum IsCurrentExecutorCheckMode: unsigned {
332
332
Legacy_NoCheckIsolated_NonCrashing,
333
333
};
334
334
static IsCurrentExecutorCheckMode isCurrentExecutorMode =
335
- Default_UseCheckIsolated_AllowCrash ;
335
+ Swift6_UseCheckIsolated_AllowCrash ;
336
336
337
337
338
338
// Shimming call to Swift runtime because Swift Embedded does not have
339
- // these symbols defined
339
+ // these symbols defined.
340
340
bool swift_bincompat_useLegacyNonCrashingExecutorChecks () {
341
341
#if !SWIFT_CONCURRENCY_EMBEDDED
342
342
return swift::runtime::bincompat::
343
343
swift_bincompat_useLegacyNonCrashingExecutorChecks ();
344
- #endif
344
+ #else
345
345
return false ;
346
+ #endif
346
347
}
347
348
348
349
// Check override of executor checking mode.
@@ -352,19 +353,17 @@ static void checkIsCurrentExecutorMode(void *context) {
352
353
353
354
// Potentially, override the platform detected mode, primarily used in tests.
354
355
#if SWIFT_STDLIB_HAS_ENVIRON
355
- if (const char *modeStr =
356
- runtime::environment::concurrencyIsCurrentExecutorLegacyModeOverride ()) {
357
- if (modeStr) {
358
- if (strcmp (modeStr, " nocrash" ) == 0 ) {
359
- useLegacyMode = true ;
360
- } else if (strcmp (modeStr, " crash" ) == 0 ) {
361
- useLegacyMode = false ;
362
- } // else, just use the platform detected mode
363
- }
356
+ if (const char *modeStr = runtime::environment::concurrencyIsCurrentExecutorLegacyModeOverride ()) {
357
+ if (strcmp (modeStr, " nocrash" ) == 0 || strcmp (modeStr, " legacy" ) == 0 ) {
358
+ useLegacyMode = true ;
359
+ } else if (strcmp (modeStr, " crash" ) == 0 || strcmp (modeStr, " swift6" ) == 0 ) {
360
+ useLegacyMode = false ;
361
+ } // else, just use the platform detected mode
364
362
}
365
363
#endif // SWIFT_STDLIB_HAS_ENVIRON
364
+
366
365
isCurrentExecutorMode = useLegacyMode ? Legacy_NoCheckIsolated_NonCrashing
367
- : Default_UseCheckIsolated_AllowCrash ;
366
+ : Swift6_UseCheckIsolated_AllowCrash ;
368
367
}
369
368
370
369
SWIFT_CC (swift)
@@ -373,6 +372,12 @@ static bool swift_task_isCurrentExecutorImpl(SerialExecutorRef expectedExecutor)
373
372
374
373
// To support old applications on apple platforms which assumed this call
375
374
// does not crash, try to use a more compatible mode for those apps.
375
+ //
376
+ // We only allow returning `false` directly from this function when operating
377
+ // in 'Legacy_NoCheckIsolated_NonCrashing' mode. If allowing crashes, we
378
+ // instead must call into 'checkIsolated' or crash directly.
379
+ //
380
+ // Whenever we confirm an executor equality, we can return true, in any mode.
376
381
static swift::once_t checkModeToken;
377
382
swift::once (checkModeToken, checkIsCurrentExecutorMode, nullptr );
378
383
@@ -382,20 +387,27 @@ static bool swift_task_isCurrentExecutorImpl(SerialExecutorRef expectedExecutor)
382
387
// the expected executor however, so we need to try a bit harder before
383
388
// we fail.
384
389
385
- // Are we expecting the main executor and are using the main thread?
386
- if (expectedExecutor.isMainExecutor () && isExecutingOnMainThread ()) {
387
- // Due to compatibility with pre-checkIsolated code, we cannot remove
388
- // this special handling. CheckIsolated can handle this if the expected
389
- // executor is the main queue / main executor, however, if we cannot call
390
- // checkIsolated we cannot rely on it to handle this.
391
- // TODO: consider removing this branch when `useCrashingCheckIsolated=true`
392
- return true ;
390
+ // Legacy special handling the main executor by detecting the main thread.
391
+ //
392
+ // When 'checkIsolated' is available it will perform a dispatch queue assertion
393
+ // against the main queue, potentially resulting in a crash (expected).
394
+ //
395
+ // In legacy mode, we cannot allow crashes here, and therefore we keep the
396
+ // special best-effort handling of the "main thread".
397
+ if (isCurrentExecutorMode == Legacy_NoCheckIsolated_NonCrashing) {
398
+ if (expectedExecutor.isMainExecutor () && isExecutingOnMainThread ()) {
399
+ return true ;
400
+ }
393
401
}
394
402
403
+ // We cannot use 'complexEquality' as it requires two executor instances,
404
+ // and we do not have a 'current' executor here.
405
+
395
406
// Otherwise, as last resort, let the expected executor check using
396
407
// external means, as it may "know" this thread is managed by it etc.
397
- if (isCurrentExecutorMode == Default_UseCheckIsolated_AllowCrash) {
398
- swift_task_checkIsolated (expectedExecutor);
408
+ if (isCurrentExecutorMode == Swift6_UseCheckIsolated_AllowCrash) {
409
+ swift_task_checkIsolated (expectedExecutor); // will crash if not same context
410
+
399
411
// checkIsolated did not crash, so we are on the right executor, after all!
400
412
return true ;
401
413
}
@@ -418,46 +430,50 @@ static bool swift_task_isCurrentExecutorImpl(SerialExecutorRef expectedExecutor)
418
430
return true ;
419
431
}
420
432
421
- // If the expected executor is "default" then we should have matched
422
- // by pointer equality already with the current executor.
423
- if (expectedExecutor.isDefaultActor ()) {
424
- // If the expected executor is a default actor, it makes no sense to try
425
- // the 'checkIsolated' call, it must be equal to the other actor, or it is
426
- // not the same isolation domain.
427
- swift_Concurrency_fatalError (0 , " Incorrect actor executor assumption" );
428
- return false ;
429
- }
430
-
431
- if (expectedExecutor.isMainExecutor () && !currentExecutor.isMainExecutor ()) {
432
- // TODO: Invoke checkIsolated() on "main" SerialQueue once it implements `checkIsolated`, otherwise messages will be sub-par and hard to address
433
- swift_Concurrency_fatalError (0 , " Incorrect actor executor assumption; Expected MainActor executor" );
434
- return false ;
435
- } else if (!expectedExecutor.isMainExecutor () && currentExecutor.isMainExecutor ()) {
436
- // TODO: Invoke checkIsolated() on "main" SerialQueue once it implements `checkIsolated`, otherwise messages will be sub-par and hard to address
437
- swift_Concurrency_fatalError (0 , " Incorrect actor executor assumption; Expected not-MainActor executor" );
438
- return false ;
439
- }
440
-
441
- if (expectedExecutor.isComplexEquality ()) {
442
- if (!swift_compareWitnessTables (
443
- reinterpret_cast <const WitnessTable*>(currentExecutor.getSerialExecutorWitnessTable ()),
444
- reinterpret_cast <const WitnessTable*>(expectedExecutor.getSerialExecutorWitnessTable ()))) {
445
- // different witness table, we cannot invoke complex equality call
433
+ // Only in legacy mode:
434
+ // We check if the current xor expected executor are the main executor.
435
+ // If so only one of them is, we know that WITHOUT 'checkIsolated' or invoking
436
+ // 'dispatch_assert_queue' we cannot be truly sure the expected/current truly
437
+ // are "on the same queue". There exists no non-crashing API to check this,
438
+ // so we PESSIMISTICALLY return false here.
439
+ //
440
+ // In Swift6 mode:
441
+ // We don't do this naive check, because we'll fall back to
442
+ // `expected.checkIsolated()` which, if it is the main executor, will invoke
443
+ // the crashing 'dispatch_assert_queue(main queue)' which will either crash
444
+ // or confirm we actually are on the main queue; or the custom expected
445
+ // executor has a chance to implement a similar queue check.
446
+ if (isCurrentExecutorMode == Legacy_NoCheckIsolated_NonCrashing) {
447
+ if ((expectedExecutor.isMainExecutor () && !currentExecutor.isMainExecutor ()) ||
448
+ (!expectedExecutor.isMainExecutor () && currentExecutor.isMainExecutor ())) {
446
449
return false ;
447
450
}
451
+ }
448
452
449
- // Avoid passing nulls to Swift for the isSame check:
450
- if (!currentExecutor.getIdentity () || !expectedExecutor.getIdentity ()) {
451
- return false ;
453
+ // Complex equality means that if two executors of the same type have some
454
+ // special logic to check if they are "actually the same".
455
+ if (expectedExecutor.isComplexEquality ()) {
456
+ if (currentExecutor.getIdentity () &&
457
+ expectedExecutor.getIdentity () &&
458
+ swift_compareWitnessTables (
459
+ reinterpret_cast <const WitnessTable *>(
460
+ currentExecutor.getSerialExecutorWitnessTable ()),
461
+ reinterpret_cast <const WitnessTable *>(
462
+ expectedExecutor.getSerialExecutorWitnessTable ()))) {
463
+
464
+ auto isSameExclusiveExecutionContextResult =
465
+ _task_serialExecutor_isSameExclusiveExecutionContext (
466
+ currentExecutor.getIdentity (), expectedExecutor.getIdentity (),
467
+ swift_getObjectType (currentExecutor.getIdentity ()),
468
+ expectedExecutor.getSerialExecutorWitnessTable ());
469
+
470
+ // if the 'isSameExclusiveExecutionContext' returned true we trust
471
+ // it and return; if it was false, we need to give checkIsolated another
472
+ // chance to check.
473
+ if (isSameExclusiveExecutionContextResult) {
474
+ return true ;
475
+ } // else, we must give 'checkIsolated' a last chance to verify isolation
452
476
}
453
-
454
- auto result = _task_serialExecutor_isSameExclusiveExecutionContext (
455
- currentExecutor.getIdentity (),
456
- expectedExecutor.getIdentity (),
457
- swift_getObjectType (currentExecutor.getIdentity ()),
458
- expectedExecutor.getSerialExecutorWitnessTable ());
459
-
460
- return result;
461
477
}
462
478
463
479
// This provides a last-resort check by giving the expected SerialExecutor the
@@ -480,21 +496,22 @@ static bool swift_task_isCurrentExecutorImpl(SerialExecutorRef expectedExecutor)
480
496
// Note that this only works because the closure in assumeIsolated is
481
497
// synchronous, and will not cause suspensions, as that would require the
482
498
// presence of a Task.
483
- // compat_invoke_swift_task_checkIsolated(expectedExecutor);
484
- if (isCurrentExecutorMode == Default_UseCheckIsolated_AllowCrash) {
485
- swift_task_checkIsolated (expectedExecutor);
499
+ if (isCurrentExecutorMode == Swift6_UseCheckIsolated_AllowCrash) {
500
+ swift_task_checkIsolated (expectedExecutor); // will crash if not same context
501
+
486
502
// The checkIsolated call did not crash, so we are on the right executor.
487
503
return true ;
488
504
}
489
505
490
- // Using legacy mode, if no explicit executor match worked, we assume `false`
506
+ // In the end, since 'checkIsolated' could not be used, so we must assume
507
+ // that the executors are not the same context.
491
508
assert (isCurrentExecutorMode == Legacy_NoCheckIsolated_NonCrashing);
492
509
return false ;
493
510
}
494
511
495
512
// / Logging level for unexpected executors:
496
- // / 0 - no logging
497
- // / 1 - warn on each instance
513
+ // / 0 - no logging -- will be IGNORED when Swift6 mode of isCurrentExecutor is used
514
+ // / 1 - warn on each instance -- will be IGNORED when Swift6 mode of isCurrentExecutor is used
498
515
// / 2 - fatal error
499
516
// /
500
517
// / NOTE: The default behavior on Apple platforms depends on the SDK version
@@ -511,9 +528,28 @@ static void checkUnexpectedExecutorLogLevel(void *context) {
511
528
if (!levelStr)
512
529
return ;
513
530
531
+ auto isCurrentExecutorLegacyMode =
532
+ swift_bincompat_useLegacyNonCrashingExecutorChecks ();
533
+
514
534
long level = strtol (levelStr, nullptr , 0 );
515
- if (level >= 0 && level < 3 )
516
- unexpectedExecutorLogLevel = level;
535
+ if (level >= 0 && level < 3 ) {
536
+ if (isCurrentExecutorLegacyMode) {
537
+ // legacy mode permits doing nothing or just logging, since the method
538
+ // used to perform the check itself is not going to crash:
539
+ unexpectedExecutorLogLevel = level;
540
+ } else {
541
+ // We are in swift6/crash mode of isCurrentExecutor which means that
542
+ // rather than returning false, that method will always CRASH when an
543
+ // executor mismatch is discovered.
544
+ //
545
+ // Thus, for clarity, we set this mode also to crashing, as runtime should
546
+ // not expect to be able to get any logging or ignoring done. In practice,
547
+ // the crash would happen before logging or "ignoring", but this should
548
+ // help avoid confusing situations like "I thought it should log" when
549
+ // debugging the runtime.
550
+ unexpectedExecutorLogLevel = 2 ;
551
+ }
552
+ }
517
553
#endif // SWIFT_STDLIB_HAS_ENVIRON
518
554
}
519
555
0 commit comments