@@ -4645,6 +4645,8 @@ static StringRef findAssociatedTypeName(const ProtocolDescriptor *protocol,
4645
4645
return StringRef ();
4646
4646
}
4647
4647
4648
+ using AssociatedTypeWitness = std::atomic<const Metadata *>;
4649
+
4648
4650
SWIFT_CC (swift)
4649
4651
static MetadataResponse
4650
4652
swift_getAssociatedTypeWitnessSlowImpl (
@@ -4668,8 +4670,8 @@ swift_getAssociatedTypeWitnessSlowImpl(
4668
4670
4669
4671
// Retrieve the witness.
4670
4672
unsigned witnessIndex = assocType - reqBase;
4671
- auto *witnessAddr = &((const Metadata * *)wtable)[witnessIndex];
4672
- auto witness = * witnessAddr;
4673
+ auto *witnessAddr = &((AssociatedTypeWitness *)wtable)[witnessIndex];
4674
+ auto witness = witnessAddr-> load (std::memory_order_acquire) ;
4673
4675
4674
4676
#if SWIFT_PTRAUTH
4675
4677
uint16_t extraDiscriminator = assocType->Flags .getExtraDiscriminator ();
@@ -4773,8 +4775,14 @@ swift_getAssociatedTypeWitnessSlowImpl(
4773
4775
if (response.State == MetadataState::Complete) {
4774
4776
// We pass type metadata around as unsigned pointers, but we sign them
4775
4777
// in witness tables, which doesn't provide all that much extra security.
4776
- initAssociatedTypeProtocolWitness (witnessAddr, assocTypeMetadata,
4777
- *assocType);
4778
+ auto valueToStore = assocTypeMetadata;
4779
+ #if SWIFT_PTRAUTH
4780
+ valueToStore = ptrauth_sign_unauthenticated (valueToStore,
4781
+ swift_ptrauth_key_associated_type,
4782
+ ptrauth_blend_discriminator (witnessAddr,
4783
+ extraDiscriminator));
4784
+ #endif
4785
+ witnessAddr->store (valueToStore, std::memory_order_release);
4778
4786
}
4779
4787
4780
4788
return response;
@@ -4791,8 +4799,8 @@ swift::swift_getAssociatedTypeWitness(MetadataRequest request,
4791
4799
4792
4800
// If the low bit of the witness is clear, it's already a metadata pointer.
4793
4801
unsigned witnessIndex = assocType - reqBase;
4794
- auto *witnessAddr = &((const void * *)wtable)[witnessIndex];
4795
- auto witness = * witnessAddr;
4802
+ auto *witnessAddr = &((const AssociatedTypeWitness *)wtable)[witnessIndex];
4803
+ auto witness = witnessAddr-> load (std::memory_order_acquire) ;
4796
4804
4797
4805
#if SWIFT_PTRAUTH
4798
4806
uint16_t extraDiscriminator = assocType->Flags .getExtraDiscriminator ();
@@ -4812,6 +4820,8 @@ swift::swift_getAssociatedTypeWitness(MetadataRequest request,
4812
4820
reqBase, assocType);
4813
4821
}
4814
4822
4823
+ using AssociatedConformanceWitness = std::atomic<void *>;
4824
+
4815
4825
SWIFT_CC (swift)
4816
4826
static const WitnessTable *swift_getAssociatedConformanceWitnessSlowImpl (
4817
4827
WitnessTable *wtable,
@@ -4837,8 +4847,8 @@ static const WitnessTable *swift_getAssociatedConformanceWitnessSlowImpl(
4837
4847
4838
4848
// Retrieve the witness.
4839
4849
unsigned witnessIndex = assocConformance - reqBase;
4840
- auto *witnessAddr = &((void * *)wtable)[witnessIndex];
4841
- auto witness = * witnessAddr;
4850
+ auto *witnessAddr = &((AssociatedConformanceWitness *)wtable)[witnessIndex];
4851
+ auto witness = witnessAddr-> load (std::memory_order_acquire) ;
4842
4852
4843
4853
#if SWIFT_PTRAUTH
4844
4854
// For associated protocols, the witness is signed with address
@@ -4897,9 +4907,18 @@ static const WitnessTable *swift_getAssociatedConformanceWitnessSlowImpl(
4897
4907
4898
4908
// The access function returns an unsigned pointer for now.
4899
4909
4900
- // We can't just use initAssociatedConformanceProtocolWitness because we
4901
- // also use this function for base protocols.
4902
- initProtocolWitness (witnessAddr, assocWitnessTable, *assocConformance);
4910
+ auto valueToStore = assocWitnessTable;
4911
+ #if SWIFT_PTRAUTH
4912
+ if (assocConformance->Flags .isSignedWithAddress ()) {
4913
+ uint16_t extraDiscriminator =
4914
+ assocConformance->Flags .getExtraDiscriminator ();
4915
+ valueToStore = ptrauth_sign_unauthenticated (valueToStore,
4916
+ swift_ptrauth_key_associated_conformance,
4917
+ ptrauth_blend_discriminator (witnessAddr,
4918
+ extraDiscriminator));
4919
+ }
4920
+ #endif
4921
+ witnessAddr->store (valueToStore, std::memory_order_release);
4903
4922
4904
4923
return assocWitnessTable;
4905
4924
}
@@ -4920,8 +4939,8 @@ const WitnessTable *swift::swift_getAssociatedConformanceWitness(
4920
4939
4921
4940
// Retrieve the witness.
4922
4941
unsigned witnessIndex = assocConformance - reqBase;
4923
- auto *witnessAddr = &((const void * *)wtable)[witnessIndex];
4924
- auto witness = * witnessAddr;
4942
+ auto *witnessAddr = &((AssociatedConformanceWitness *)wtable)[witnessIndex];
4943
+ auto witness = witnessAddr-> load (std::memory_order_acquire) ;
4925
4944
4926
4945
#if SWIFT_PTRAUTH
4927
4946
uint16_t extraDiscriminator = assocConformance->Flags .getExtraDiscriminator ();
0 commit comments