Skip to content

Avoid using String while holding an internal libobjc lock. #938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions Sources/Testing/Discovery+Platform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ struct SectionBounds: Sendable {
#if SWT_TARGET_OS_APPLE
// MARK: - Apple implementation

extension SectionBounds.Kind {
/// The Mach-O segment and section name for this instance as a pair of
/// null-terminated UTF-8 C strings and pass them to a function.
///
/// The values of this property within this function are instances of
/// `StaticString` rather than `String` because the latter's inner storage is
/// sometimes Objective-C-backed and touching it here can cause a recursive
/// access to an internal libobjc lock, whereas `StaticString`'s internal
/// storage is immediately available.
fileprivate var segmentAndSectionName: (segmentName: StaticString, sectionName: StaticString) {
switch self {
case .testContent:
("__DATA_CONST", "__swift5_tests")
case .typeMetadata:
("__TEXT", "__swift5_types")
}
}
}

/// An array containing all of the test content section bounds known to the
/// testing library.
private let _sectionBounds = Locked<[SectionBounds.Kind: [SectionBounds]]>()
Expand Down Expand Up @@ -77,18 +96,17 @@ private let _startCollectingSectionBounds: Void = {

// If this image contains the Swift section(s) we need, acquire the lock and
// store the section's bounds.
func findSectionBounds(forSectionNamed segmentName: String, _ sectionName: String, ofKind kind: SectionBounds.Kind) {
for kind in SectionBounds.Kind.allCases {
let (segmentName, sectionName) = kind.segmentAndSectionName
var size = CUnsignedLong(0)
if let start = getsectiondata(mh, segmentName, sectionName, &size), size > 0 {
if let start = getsectiondata(mh, segmentName.utf8Start, sectionName.utf8Start, &size), size > 0 {
let buffer = UnsafeRawBufferPointer(start: start, count: Int(clamping: size))
let sb = SectionBounds(imageAddress: mh, buffer: buffer)
_sectionBounds.withLock { sectionBounds in
sectionBounds[kind]!.append(sb)
}
}
}
findSectionBounds(forSectionNamed: "__DATA_CONST", "__swift5_tests", ofKind: .testContent)
findSectionBounds(forSectionNamed: "__TEXT", "__swift5_types", ofKind: .typeMetadata)
}

#if _runtime(_ObjC)
Expand All @@ -97,7 +115,7 @@ private let _startCollectingSectionBounds: Void = {
}
#else
_dyld_register_func_for_add_image { mh, _ in
addSectionBounds(from: mh)
addSectionBounds(from: mh!)
}
#endif
}()
Expand Down