Skip to content

Commit f1850ef

Browse files
authored
Only store Mach header addresses for images containing Swift data. (#351)
We currently try to do minimal work in our `objc_addLoadImageFunc()` callback on Darwin—we acquire a lock, add each reported Mach header to a list, relinquish the lock, and immediately return. We then look for appropriate section data on demand in `enumerateTypeMetadataSections()`. This is a problem if an image is unloaded after the callback to `objc_addLoadImageFunc()` returns but before we enumerate images. Images containing Swift do not support unloading, but pure C images might find themselves in this situation. This PR checks for the presence of a `"__swift5_types"` section before adding an image to the list, so references to images that could be unloaded won't be held indefinitely. Resolves rdar://124426864. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent fd53525 commit f1850ef

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

Sources/TestingInternals/Discovery.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,18 @@ static SWTMachHeaderList getMachHeaders(void) {
233233
machHeaders->reserve(_dyld_image_count());
234234

235235
objc_addLoadImageFunc([] (const mach_header *mh) {
236-
os_unfair_lock_lock(&lock); {
237-
machHeaders->push_back(reinterpret_cast<SWTMachHeaderList::value_type>(mh));
238-
} os_unfair_lock_unlock(&lock);
236+
auto mhn = reinterpret_cast<SWTMachHeaderList::value_type>(mh);
237+
238+
// Only store the mach header address if the image contains Swift data.
239+
// Swift does not support unloading images, but images that do not contain
240+
// Swift code may be unloaded at runtime and later crash
241+
// the testing library when it calls enumerateTypeMetadataSections().
242+
unsigned long size = 0;
243+
if (getsectiondata(mhn, SEG_TEXT, "__swift5_types", &size)) {
244+
os_unfair_lock_lock(&lock); {
245+
machHeaders->push_back(mhn);
246+
} os_unfair_lock_unlock(&lock);
247+
}
239248
});
240249
});
241250

0 commit comments

Comments
 (0)