@@ -4654,6 +4654,8 @@ static StringRef findAssociatedTypeName(const ProtocolDescriptor *protocol,
4654
4654
return StringRef ();
4655
4655
}
4656
4656
4657
+ using AssociatedTypeWitness = std::atomic<const Metadata *>;
4658
+
4657
4659
SWIFT_CC (swift)
4658
4660
static MetadataResponse
4659
4661
swift_getAssociatedTypeWitnessSlowImpl (
@@ -4677,8 +4679,8 @@ swift_getAssociatedTypeWitnessSlowImpl(
4677
4679
4678
4680
// Retrieve the witness.
4679
4681
unsigned witnessIndex = assocType - reqBase;
4680
- auto *witnessAddr = &((const Metadata * *)wtable)[witnessIndex];
4681
- auto witness = * witnessAddr;
4682
+ auto *witnessAddr = &((AssociatedTypeWitness *)wtable)[witnessIndex];
4683
+ auto witness = witnessAddr-> load (std::memory_order_acquire) ;
4682
4684
4683
4685
#if SWIFT_PTRAUTH
4684
4686
uint16_t extraDiscriminator = assocType->Flags .getExtraDiscriminator ();
@@ -4781,8 +4783,14 @@ swift_getAssociatedTypeWitnessSlowImpl(
4781
4783
if (response.State == MetadataState::Complete) {
4782
4784
// We pass type metadata around as unsigned pointers, but we sign them
4783
4785
// in witness tables, which doesn't provide all that much extra security.
4784
- initAssociatedTypeProtocolWitness (witnessAddr, assocTypeMetadata,
4785
- *assocType);
4786
+ auto valueToStore = assocTypeMetadata;
4787
+ #if SWIFT_PTRAUTH
4788
+ valueToStore = ptrauth_sign_unauthenticated (valueToStore,
4789
+ swift_ptrauth_key_associated_type,
4790
+ ptrauth_blend_discriminator (witnessAddr,
4791
+ extraDiscriminator));
4792
+ #endif
4793
+ witnessAddr->store (valueToStore, std::memory_order_release);
4786
4794
}
4787
4795
4788
4796
return response;
@@ -4799,8 +4807,8 @@ swift::swift_getAssociatedTypeWitness(MetadataRequest request,
4799
4807
4800
4808
// If the low bit of the witness is clear, it's already a metadata pointer.
4801
4809
unsigned witnessIndex = assocType - reqBase;
4802
- auto *witnessAddr = &((const void * *)wtable)[witnessIndex];
4803
- auto witness = * witnessAddr;
4810
+ auto *witnessAddr = &((const AssociatedTypeWitness *)wtable)[witnessIndex];
4811
+ auto witness = witnessAddr-> load (std::memory_order_acquire) ;
4804
4812
4805
4813
#if SWIFT_PTRAUTH
4806
4814
uint16_t extraDiscriminator = assocType->Flags .getExtraDiscriminator ();
@@ -4819,6 +4827,8 @@ swift::swift_getAssociatedTypeWitness(MetadataRequest request,
4819
4827
reqBase, assocType);
4820
4828
}
4821
4829
4830
+ using AssociatedConformanceWitness = std::atomic<void *>;
4831
+
4822
4832
SWIFT_CC (swift)
4823
4833
static const WitnessTable *swift_getAssociatedConformanceWitnessSlowImpl (
4824
4834
WitnessTable *wtable,
@@ -4844,8 +4854,8 @@ static const WitnessTable *swift_getAssociatedConformanceWitnessSlowImpl(
4844
4854
4845
4855
// Retrieve the witness.
4846
4856
unsigned witnessIndex = assocConformance - reqBase;
4847
- auto *witnessAddr = &((void * *)wtable)[witnessIndex];
4848
- auto witness = * witnessAddr;
4857
+ auto *witnessAddr = &((AssociatedConformanceWitness *)wtable)[witnessIndex];
4858
+ auto witness = witnessAddr-> load (std::memory_order_acquire) ;
4849
4859
4850
4860
#if SWIFT_PTRAUTH
4851
4861
// For associated protocols, the witness is signed with address
@@ -4903,9 +4913,18 @@ static const WitnessTable *swift_getAssociatedConformanceWitnessSlowImpl(
4903
4913
4904
4914
// The access function returns an unsigned pointer for now.
4905
4915
4906
- // We can't just use initAssociatedConformanceProtocolWitness because we
4907
- // also use this function for base protocols.
4908
- initProtocolWitness (witnessAddr, assocWitnessTable, *assocConformance);
4916
+ auto valueToStore = assocWitnessTable;
4917
+ #if SWIFT_PTRAUTH
4918
+ if (assocConformance->Flags .isSignedWithAddress ()) {
4919
+ uint16_t extraDiscriminator =
4920
+ assocConformance->Flags .getExtraDiscriminator ();
4921
+ valueToStore = ptrauth_sign_unauthenticated (valueToStore,
4922
+ swift_ptrauth_key_associated_conformance,
4923
+ ptrauth_blend_discriminator (witnessAddr,
4924
+ extraDiscriminator));
4925
+ }
4926
+ #endif
4927
+ witnessAddr->store (valueToStore, std::memory_order_release);
4909
4928
4910
4929
return assocWitnessTable;
4911
4930
}
@@ -4926,8 +4945,8 @@ const WitnessTable *swift::swift_getAssociatedConformanceWitness(
4926
4945
4927
4946
// Retrieve the witness.
4928
4947
unsigned witnessIndex = assocConformance - reqBase;
4929
- auto *witnessAddr = &((const void * *)wtable)[witnessIndex];
4930
- auto witness = * witnessAddr;
4948
+ auto *witnessAddr = &((AssociatedConformanceWitness *)wtable)[witnessIndex];
4949
+ auto witness = witnessAddr-> load (std::memory_order_acquire) ;
4931
4950
4932
4951
#if SWIFT_PTRAUTH
4933
4952
uint16_t extraDiscriminator = assocConformance->Flags .getExtraDiscriminator ();
0 commit comments