|
44 | 44 | # include "swift/Runtime/ObjCBridge.h"
|
45 | 45 | # include <dlfcn.h>
|
46 | 46 | #endif
|
| 47 | +#if SWIFT_STDLIB_HAS_MALLOC_TYPE |
| 48 | +# include <malloc_type_private.h> |
| 49 | +#endif |
47 | 50 | #include "Leaks.h"
|
48 | 51 |
|
49 | 52 | using namespace swift;
|
@@ -115,12 +118,118 @@ static HeapObject *_swift_tryRetain_(HeapObject *object)
|
115 | 118 | return _ ## name ## _ args; \
|
116 | 119 | } while(0)
|
117 | 120 |
|
| 121 | +#if SWIFT_STDLIB_HAS_MALLOC_TYPE |
| 122 | +static malloc_type_summary_t |
| 123 | +computeMallocTypeSummary(const HeapMetadata *heapMetadata) { |
| 124 | + assert(isHeapMetadataKind(heapMetadata->getKind())); |
| 125 | + auto *classMetadata = heapMetadata->getClassObject(); |
| 126 | + auto *typeDesc = heapMetadata->getTypeContextDescriptor(); |
| 127 | + |
| 128 | + malloc_type_summary_t summary = {}; |
| 129 | + |
| 130 | + // Objc |
| 131 | + if (classMetadata && classMetadata->isPureObjC()) { |
| 132 | + summary.type_kind = MALLOC_TYPE_KIND_OBJC; |
| 133 | + return summary; |
| 134 | + } |
| 135 | + |
| 136 | + // Runtime internal and unclassified |
| 137 | + if (!typeDesc) { |
| 138 | + summary.type_kind = MALLOC_TYPE_KIND_CXX; |
| 139 | + return summary; |
| 140 | + } |
| 141 | + |
| 142 | + // Swift |
| 143 | + summary.type_kind = MALLOC_TYPE_KIND_SWIFT; |
| 144 | + |
| 145 | + bool isGenericData = true; |
| 146 | + for (auto &field : *typeDesc->Fields.get()) { |
| 147 | + if (field.isIndirectCase()) { |
| 148 | + isGenericData = false; |
| 149 | + if (field.isVar()) |
| 150 | + summary.layout_semantics.data_pointer = true; |
| 151 | + else |
| 152 | + summary.layout_semantics.immutable_pointer = true; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + if (classMetadata->Flags & ClassFlags::UsesSwiftRefcounting) { |
| 157 | + summary.layout_semantics.reference_count = true; |
| 158 | + } else { |
| 159 | + summary.layout_semantics.generic_data = isGenericData; |
| 160 | + } |
| 161 | + |
| 162 | + return summary; |
| 163 | + |
| 164 | +// FIXME: these are all the things we are potentially interested in |
| 165 | +// typedef struct { |
| 166 | +// bool data_pointer : 1; |
| 167 | +// bool struct_pointer : 1; |
| 168 | +// bool immutable_pointer : 1; |
| 169 | +// bool anonymous_pointer : 1; |
| 170 | +// bool reference_count : 1; |
| 171 | +// bool resource_handle : 1; |
| 172 | +// bool spatial_bounds : 1; |
| 173 | +// bool tainted_data : 1; |
| 174 | +// bool generic_data : 1; |
| 175 | +// uint16_t unused : 7; |
| 176 | +// } malloc_type_layout_semantics_t; |
| 177 | +} |
| 178 | + |
| 179 | +struct MallocTypeCacheEntry { |
| 180 | +// union malloc_type_descriptor_t { |
| 181 | +// struct { |
| 182 | +// uint32_t hash; |
| 183 | +// malloc_type_summary_t summary; |
| 184 | +// }; |
| 185 | +// malloc_type_id_t type_id; |
| 186 | +// }; |
| 187 | + malloc_type_descriptor_t desc; |
| 188 | + |
| 189 | + friend llvm::hash_code hash_value(const MallocTypeCacheEntry &entry) { |
| 190 | + return hash_value(entry.desc.hash); |
| 191 | + } |
| 192 | + bool matchesKey(uint32_t key) const { return desc.hash == key; } |
| 193 | +}; |
| 194 | +static ConcurrentReadableHashMap<MallocTypeCacheEntry> MallocTypes; |
| 195 | + |
| 196 | +static malloc_type_id_t getMallocTypeId(const HeapMetadata *heapMetadata) { |
| 197 | + uint64_t metadataPtrBits = reinterpret_cast<uint64_t>(heapMetadata); |
| 198 | + uint32_t key = (metadataPtrBits >> 32) ^ (metadataPtrBits >> 0); |
| 199 | + |
| 200 | + { |
| 201 | + auto snapshot = MallocTypes.snapshot(); |
| 202 | + if (auto *entry = snapshot.find(key)) |
| 203 | + return entry->desc.type_id; |
| 204 | + } |
| 205 | + |
| 206 | + malloc_type_descriptor_t desc = { |
| 207 | + .hash = key, |
| 208 | + .summary = computeMallocTypeSummary(heapMetadata) |
| 209 | + }; |
| 210 | + |
| 211 | + MallocTypes.getOrInsert( |
| 212 | + key, [desc](MallocTypeCacheEntry *entry, bool created) { |
| 213 | + if (created) |
| 214 | + entry->desc = desc; |
| 215 | + return true; |
| 216 | + }); |
| 217 | + |
| 218 | + return desc.type_id; |
| 219 | +} |
| 220 | +#endif // SWIFT_STDLIB_HAS_MALLOC_TYPE |
| 221 | + |
118 | 222 | static HeapObject *_swift_allocObject_(HeapMetadata const *metadata,
|
119 | 223 | size_t requiredSize,
|
120 | 224 | size_t requiredAlignmentMask) {
|
121 | 225 | assert(isAlignmentMask(requiredAlignmentMask));
|
| 226 | +#if SWIFT_STDLIB_HAS_MALLOC_TYPE |
| 227 | + auto object = reinterpret_cast<HeapObject *>(swift_slowAllocTyped( |
| 228 | + requiredSize, requiredAlignmentMask, getMallocTypeId(metadata))); |
| 229 | +#else |
122 | 230 | auto object = reinterpret_cast<HeapObject *>(
|
123 | 231 | swift_slowAlloc(requiredSize, requiredAlignmentMask));
|
| 232 | +#endif |
124 | 233 |
|
125 | 234 | // NOTE: this relies on the C++17 guaranteed semantics of no null-pointer
|
126 | 235 | // check on the placement new allocator which we have observed on Windows,
|
|
0 commit comments