Skip to content

Commit 3c93f6f

Browse files
authored
Avoid calling malloc() while holding a lock that may block dyld or libobjc. (#523)
This PR pre-sizes the vector result of `getMachHeaders()` on Darwin (used during test discovery) so that it is very unlikely that it will need to be reallocated while holding the function's lock. If `malloc()` is called while this lock is held, there is a theoretical possibility of deadlock with the Objective-C runtime or dyld itself if `malloc()` ends up calling into either. ### 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 8ed959b commit 3c93f6f

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

Sources/_TestingInternals/Discovery.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ static SWTMachHeaderList getMachHeaders(void) {
260260
// After the first call sets up the loader hook, all calls take the lock and
261261
// make a copy of whatever has been loaded so far.
262262
SWTMachHeaderList result;
263+
result.reserve(_dyld_image_count());
263264
os_unfair_lock_lock(&lock); {
264265
result = *machHeaders;
265266
} os_unfair_lock_unlock(&lock);

0 commit comments

Comments
 (0)