Skip to content

Commit 6d0a092

Browse files
Merge pull request #63380 from aschwaighofer/remove_ub_in_relative_witness_table_description
Remove undefined behavior in RelativeWitnessTable::getDescription()
2 parents 8fde58f + 0f9bfac commit 6d0a092

File tree

5 files changed

+16
-17
lines changed

5 files changed

+16
-17
lines changed

include/swift/ABI/Metadata.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,13 +1661,6 @@ class TargetRelativeWitnessTable {
16611661

16621662
public:
16631663
const TargetProtocolConformanceDescriptor<Runtime> *getDescription() const {
1664-
// "Look through" dynamically allocated witness tables.
1665-
uintptr_t selfValue = (uintptr_t)this;
1666-
if (selfValue & 0x1) {
1667-
selfValue = selfValue & ~((uintptr_t)0x1);
1668-
auto selfAddr = (TargetRelativeWitnessTable<Runtime> **)selfValue;
1669-
return (*selfAddr)->Description;
1670-
}
16711664
return Description;
16721665
}
16731666
};

stdlib/public/runtime/AnyHashableSupport.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ findHashableBaseTypeImpl(const Metadata *type) {
9999
}
100100
// By this point, `type` is known to conform to `Hashable`.
101101
#if SWIFT_STDLIB_USE_RELATIVE_PROTOCOL_WITNESS_TABLES
102-
const auto *conformance = reinterpret_cast<const RelativeWitnessTable*>(
103-
witnessTable)->getDescription();
102+
const auto *conformance = lookThroughOptionalConditionalWitnessTable(
103+
reinterpret_cast<const RelativeWitnessTable*>(witnessTable))
104+
->getDescription();
104105
#else
105106
const auto *conformance = witnessTable->getDescription();
106107
#endif

stdlib/public/runtime/Metadata.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5847,15 +5847,15 @@ swift::swift_getAssociatedTypeWitness(MetadataRequest request,
58475847
reqBase, assocType);
58485848
}
58495849

5850-
RelativeWitnessTable *
5851-
lookThroughOptionalConditionalWitnessTable(RelativeWitnessTable *wtable) {
5850+
RelativeWitnessTable *swift::lookThroughOptionalConditionalWitnessTable(
5851+
const RelativeWitnessTable *wtable) {
58525852
uintptr_t conditional_wtable = (uintptr_t)wtable;
58535853
if (conditional_wtable & 0x1) {
58545854
conditional_wtable = conditional_wtable & ~(uintptr_t)(0x1);
58555855
conditional_wtable = (uintptr_t)*(void**)conditional_wtable;
58565856
}
5857-
wtable = (RelativeWitnessTable*)conditional_wtable;
5858-
return wtable;
5857+
auto table = (RelativeWitnessTable*)conditional_wtable;
5858+
return table;
58595859
}
58605860

58615861
SWIFT_CC(swift)

stdlib/public/runtime/MetadataCache.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
namespace swift {
3333

34+
RelativeWitnessTable *lookThroughOptionalConditionalWitnessTable(const RelativeWitnessTable *);
35+
3436
#if !SWIFT_STDLIB_PASSTHROUGH_METADATA_ALLOCATOR
3537

3638
class MetadataAllocator : public llvm::AllocatorBase<MetadataAllocator> {
@@ -450,10 +452,10 @@ class MetadataCacheKey {
450452
if (awt == bwt)
451453
return 0;
452454
#if SWIFT_STDLIB_USE_RELATIVE_PROTOCOL_WITNESS_TABLES
453-
auto *aDescription =
454-
reinterpret_cast<const RelativeWitnessTable*>(awt)->getDescription();
455-
auto *bDescription =
456-
reinterpret_cast<const RelativeWitnessTable*>(bwt)->getDescription();
455+
auto *aDescription = lookThroughOptionalConditionalWitnessTable(
456+
reinterpret_cast<const RelativeWitnessTable*>(awt))->getDescription();
457+
auto *bDescription = lookThroughOptionalConditionalWitnessTable(
458+
reinterpret_cast<const RelativeWitnessTable*>(bwt))->getDescription();
457459
#else
458460
auto *aDescription = awt->getDescription();
459461
auto *bDescription = bwt->getDescription();

stdlib/public/runtime/Private.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,9 @@ class TypeInfo {
663663
const ProtocolRequirement *reqBase,
664664
const ProtocolRequirement *assocConformance);
665665

666+
RelativeWitnessTable *
667+
lookThroughOptionalConditionalWitnessTable(const RelativeWitnessTable *wtable);
668+
666669
#if SWIFT_OBJC_INTEROP
667670
/// Returns a retained Quick Look representation object an Obj-C object.
668671
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL

0 commit comments

Comments
 (0)