Skip to content

Commit 6dd141a

Browse files
committed
Replace dlsym of swift_task_isCurrentGlobalActor with a constructor hook
Following the approach taken with the concurrency-specific type descriptors, register a hook function for the "is current global actor" check used for isolated conformances.
1 parent c266a09 commit 6dd141a

File tree

13 files changed

+53
-18
lines changed

13 files changed

+53
-18
lines changed

include/swift/ABI/MetadataValues.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,10 @@ namespace SpecialPointerAuthDiscriminators {
17691769

17701770
/// Isolated deinit body function pointer
17711771
const uint16_t DeinitWorkFunction = 0x8438; // = 33848
1772+
1773+
/// IsCurrentGlobalActor function used between the Swift runtime and
1774+
/// concurrency runtime.
1775+
const uint16_t IsCurrentGlobalActorFunction = 0xd1b8; // = 53688
17721776
}
17731777

17741778
/// The number of arguments that will be passed directly to a generic

include/swift/Runtime/Config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,9 @@ extern uintptr_t __COMPATIBILITY_LIBRARIES_CANNOT_CHECK_THE_IS_SWIFT_BIT_DIRECTL
327327
#define __ptrauth_swift_deinit_work_function \
328328
__ptrauth(ptrauth_key_function_pointer, 1, \
329329
SpecialPointerAuthDiscriminators::DeinitWorkFunction)
330+
#define __ptrauth_swift_is_global_actor_function \
331+
__ptrauth(ptrauth_key_function_pointer, 1, \
332+
SpecialPointerAuthDiscriminators::IsCurrentGlobalActorFunction)
330333

331334
#if __has_attribute(ptrauth_struct)
332335
#define swift_ptrauth_struct(key, discriminator) \
@@ -368,6 +371,7 @@ extern uintptr_t __COMPATIBILITY_LIBRARIES_CANNOT_CHECK_THE_IS_SWIFT_BIT_DIRECTL
368371
#define swift_ptrauth_sign_opaque_modify_resume_function(__fn, __buffer) (__fn)
369372
#define __ptrauth_swift_type_layout_string
370373
#define __ptrauth_swift_deinit_work_function
374+
#define __ptrauth_swift_is_global_actor_function
371375
#define swift_ptrauth_struct(key, discriminator)
372376
#define swift_ptrauth_struct_derived(from)
373377
#endif

include/swift/Runtime/Metadata.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,11 +1057,20 @@ struct ConcurrencyStandardTypeDescriptors {
10571057
#include "swift/Demangling/StandardTypesMangling.def"
10581058
};
10591059

1060-
/// Register the type descriptors with standard manglings from the Concurrency
1061-
/// runtime. The passed-in struct must be immortal.
1060+
/// Function that determines whether we are executing on the given global
1061+
/// actor. The metadata is for the global actor type, and the witness table
1062+
/// is the conformance of that type to the GlobalActor protocol.
1063+
typedef bool (* SWIFT_CC(swift) IsCurrentGlobalActor)(const Metadata *, const WitnessTable *);
1064+
1065+
/// Register various concurrency-related data and hooks needed in the Swift
1066+
/// standard library / runtime. This includes type descriptors with standard
1067+
/// manglings from the Concurrency runtime as well as a hook to check whether
1068+
/// we are running on a specific global actor. Any pointers passed in here must
1069+
/// be immortal.
10621070
SWIFT_RUNTIME_STDLIB_SPI
1063-
void _swift_registerConcurrencyStandardTypeDescriptors(
1064-
const ConcurrencyStandardTypeDescriptors *descriptors);
1071+
void _swift_registerConcurrencyRuntime(
1072+
const ConcurrencyStandardTypeDescriptors *descriptors,
1073+
IsCurrentGlobalActor isCurrentGlobalActor);
10651074

10661075
/// Initialize the value witness table for a struct using the provided like type
10671076
/// as the basis for the layout.

stdlib/public/Concurrency/Setup.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
MANGLING, DESCRIPTOR_MANGLING_SUFFIX(KIND));
3434
#include "swift/Demangling/StandardTypesMangling.def"
3535

36+
// Defined in Swift, redeclared here so we can register it with the runtime.
37+
extern "C" SWIFT_CC(swift)
38+
bool _swift_task_isCurrentGlobalActor(
39+
const swift::Metadata *, const swift::WitnessTable *);
40+
3641
// Register our type descriptors with standard manglings when the concurrency
3742
// runtime is loaded. This allows the runtime to quickly resolve those standard
3843
// manglings.
@@ -43,5 +48,7 @@ __attribute__((constructor)) static void setupStandardConcurrencyDescriptors() {
4348
&DESCRIPTOR_MANGLING(MANGLING, DESCRIPTOR_MANGLING_SUFFIX(KIND)),
4449
#include "swift/Demangling/StandardTypesMangling.def"
4550
};
46-
_swift_registerConcurrencyStandardTypeDescriptors(&descriptors);
51+
_swift_registerConcurrencyRuntime(
52+
&descriptors,
53+
&_swift_task_isCurrentGlobalActor);
4754
}

stdlib/public/Concurrency/Task.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,8 +1549,7 @@ internal func _taskIsCurrentExecutor(
15491549

15501550
extension GlobalActor {
15511551
@available(SwiftStdlib 6.2, *)
1552-
@_silgen_name("swift_task_isCurrentGlobalActor")
1553-
@usableFromInline
1552+
@_silgen_name("_swift_task_isCurrentGlobalActor")
15541553
internal static func _taskIsCurrentGlobalActor() -> Bool {
15551554
let executor = unsafe sharedUnownedExecutor
15561555
return unsafe _taskIsCurrentExecutor(executor: executor.executor, flags: 0)

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,9 +1053,17 @@ _findContextDescriptor(Demangle::NodePointer node,
10531053
return foundContext;
10541054
}
10551055

1056-
void swift::_swift_registerConcurrencyStandardTypeDescriptors(
1057-
const ConcurrencyStandardTypeDescriptors *descriptors) {
1056+
/// Function to check whether we're currently running on the given global
1057+
/// actor.
1058+
bool (* __ptrauth_swift_is_global_actor_function SWIFT_CC(swift)
1059+
swift::_swift_task_isCurrentGlobalActorHook)(
1060+
const Metadata *, const WitnessTable *);
1061+
1062+
void swift::_swift_registerConcurrencyRuntime(
1063+
const ConcurrencyStandardTypeDescriptors *descriptors,
1064+
IsCurrentGlobalActor isCurrentGlobalActor) {
10581065
concurrencyDescriptors = descriptors;
1066+
_swift_task_isCurrentGlobalActorHook = isCurrentGlobalActor;
10591067
}
10601068

10611069
#pragma mark Protocol descriptor cache

stdlib/public/runtime/Private.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,13 @@ class TypeInfo {
751751
id _quickLookObjectForPointer(void *value);
752752
#endif
753753

754+
/// Hook function that calls into the concurrency library to check whether
755+
/// we are currently executing the given global actor.
756+
SWIFT_RUNTIME_LIBRARY_VISIBILITY
757+
extern bool (* __ptrauth_swift_is_global_actor_function SWIFT_CC(swift)
758+
_swift_task_isCurrentGlobalActorHook)(
759+
const Metadata *, const WitnessTable *);
760+
754761
} // end namespace swift
755762

756763
#endif /* SWIFT_RUNTIME_PRIVATE_H */

stdlib/public/runtime/ProtocolConformance.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,13 +1190,11 @@ static bool _isExecutingInIsolationOfConformance(
11901190

11911191
// The concurrency library provides a function to check whether we
11921192
// are executing on the given global actor.
1193-
auto isCurrentGlobalActor = SWIFT_LAZY_CONSTANT(reinterpret_cast<bool (*)(const Metadata *, const WitnessTable *)>(
1194-
dlsym(RTLD_DEFAULT, "swift_task_isCurrentGlobalActor")));
1195-
if (!isCurrentGlobalActor)
1193+
if (!_swift_task_isCurrentGlobalActorHook)
11961194
return false;
11971195

11981196
// Check whether we are running on this global actor.
1199-
return isCurrentGlobalActor(globalActorType, globalActorWitnessTable);
1197+
return _swift_task_isCurrentGlobalActorHook(globalActorType, globalActorWitnessTable);
12001198
}
12011199

12021200
static bool _checkConformanceIsolation(

test/abi/Inputs/macOS/arm64/concurrency/baseline-asserts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,6 @@ _swift_task_getMainExecutor
997997
_swift_task_hasTaskGroupStatusRecord
998998
_swift_task_isCancelled
999999
_swift_task_isCurrentExecutor
1000-
_swift_task_isCurrentGlobalActor
10011000
_swift_task_isOnExecutor
10021001
_swift_task_isOnExecutor_hook
10031002
_swift_task_localValueGet

test/abi/Inputs/macOS/arm64/stdlib/baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13415,7 +13415,7 @@ __swift_makeAnyHashableUpcastingToHashableBaseType
1341513415
__swift_modifyAtReferenceWritableKeyPath_impl
1341613416
__swift_modifyAtWritableKeyPath_impl
1341713417
__swift_objcClassUsesNativeSwiftReferenceCounting
13418-
__swift_registerConcurrencyStandardTypeDescriptors
13418+
__swift_registerConcurrencyRuntime
1341913419
__swift_release
1342013420
__swift_release_n
1342113421
__swift_reportFatalErrorsToDebugger

test/abi/Inputs/macOS/arm64/stdlib/baseline-asserts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13504,7 +13504,7 @@ __swift_makeAnyHashableUpcastingToHashableBaseType
1350413504
__swift_modifyAtReferenceWritableKeyPath_impl
1350513505
__swift_modifyAtWritableKeyPath_impl
1350613506
__swift_objcClassUsesNativeSwiftReferenceCounting
13507-
__swift_registerConcurrencyStandardTypeDescriptors
13507+
__swift_registerConcurrencyRuntime
1350813508
__swift_release
1350913509
__swift_release_n
1351013510
__swift_reportFatalErrorsToDebugger

test/abi/Inputs/macOS/x86_64/stdlib/baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13373,7 +13373,7 @@ __swift_makeAnyHashableUpcastingToHashableBaseType
1337313373
__swift_modifyAtReferenceWritableKeyPath_impl
1337413374
__swift_modifyAtWritableKeyPath_impl
1337513375
__swift_objcClassUsesNativeSwiftReferenceCounting
13376-
__swift_registerConcurrencyStandardTypeDescriptors
13376+
__swift_registerConcurrencyRuntime
1337713377
__swift_release
1337813378
__swift_release_n
1337913379
__swift_reportFatalErrorsToDebugger

test/abi/Inputs/macOS/x86_64/stdlib/baseline-asserts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13462,7 +13462,7 @@ __swift_makeAnyHashableUpcastingToHashableBaseType
1346213462
__swift_modifyAtReferenceWritableKeyPath_impl
1346313463
__swift_modifyAtWritableKeyPath_impl
1346413464
__swift_objcClassUsesNativeSwiftReferenceCounting
13465-
__swift_registerConcurrencyStandardTypeDescriptors
13465+
__swift_registerConcurrencyRuntime
1346613466
__swift_release
1346713467
__swift_release_n
1346813468
__swift_reportFatalErrorsToDebugger

0 commit comments

Comments
 (0)