Skip to content

Simplify statically-linked section discovery. #940

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 5, 2025
Merged
Show file tree
Hide file tree
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
12 changes: 4 additions & 8 deletions Sources/Testing/Discovery+Platform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct SectionBounds: Sendable {

/// An enumeration describing the different sections discoverable by the
/// testing library.
enum Kind: Equatable, Hashable, CaseIterable {
enum Kind: Int, Equatable, Hashable, CaseIterable {
/// The test content metadata section.
case testContent

Expand Down Expand Up @@ -285,13 +285,9 @@ private func _sectionBounds(_ kind: SectionBounds.Kind) -> [SectionBounds] {
/// - Returns: A structure describing the bounds of the type metadata section
/// contained in the same image as the testing library itself.
private func _sectionBounds(_ kind: SectionBounds.Kind) -> CollectionOfOne<SectionBounds> {
let (sectionBegin, sectionEnd) = switch kind {
case .testContent:
SWTTestContentSectionBounds
case .typeMetadata:
SWTTypeMetadataSectionBounds
}
let buffer = UnsafeRawBufferPointer(start: sectionBegin, count: max(0, sectionEnd - sectionBegin))
var (baseAddress, count): (UnsafeRawPointer?, Int) = (nil, 0)
swt_getStaticallyLinkedSectionBounds(kind.rawValue, &baseAddress, &count)
let buffer = UnsafeRawBufferPointer(start: baseAddress, count: count)
let sb = SectionBounds(imageAddress: nil, buffer: buffer)
return CollectionOfOne(sb)
}
Expand Down
14 changes: 9 additions & 5 deletions Sources/_TestingInternals/Discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "Discovery.h"

#include <algorithm>
#include <cstdint>
#include <cstring>
#include <type_traits>
Expand All @@ -35,13 +36,16 @@ static const char typeMetadataSectionBegin = 0;
static const char& typeMetadataSectionEnd = typeMetadataSectionBegin;
#endif

const void *_Nonnull const SWTTestContentSectionBounds[2] = {
&testContentSectionBegin, &testContentSectionEnd
static constexpr const char *const staticallyLinkedSectionBounds[][2] = {
{ &testContentSectionBegin, &testContentSectionEnd },
{ &typeMetadataSectionBegin, &typeMetadataSectionEnd },
};

const void *_Nonnull const SWTTypeMetadataSectionBounds[2] = {
&typeMetadataSectionBegin, &typeMetadataSectionEnd
};
void swt_getStaticallyLinkedSectionBounds(size_t kind, const void **outSectionBegin, size_t *outByteCount) {
auto [sectionBegin, sectionEnd] = staticallyLinkedSectionBounds[kind];
*outSectionBegin = sectionBegin;
*outByteCount = std::distance(sectionBegin, sectionEnd);
}
#endif

#pragma mark - Swift ABI
Expand Down
17 changes: 6 additions & 11 deletions Sources/_TestingInternals/include/Discovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,18 @@ SWT_IMPORT_FROM_STDLIB void swift_enumerateAllMetadataSections(

#pragma mark - Statically-linked section bounds

/// The bounds of the test content section statically linked into the image
/// containing Swift Testing.
/// Get the bounds of a statically linked section in this image.
///
/// - Note: This symbol is _declared_, but not _defined_, on platforms with
/// dynamic linking because the `SWT_NO_DYNAMIC_LINKING` C++ macro (not the
/// Swift compiler conditional of the same name) is not consistently declared
/// when Swift files import the `_TestingInternals` C++ module.
SWT_EXTERN const void *_Nonnull const SWTTestContentSectionBounds[2];

/// The bounds of the type metadata section statically linked into the image
/// containing Swift Testing.
/// - Parameters:
/// - kind: The value of `SectionBounds.Kind.rawValue` for the given section.
/// - outSectionBegin: On return, a pointer to the first byte of the section.
/// - outByteCount: On return, the number of bytes in the section.
///
/// - Note: This symbol is _declared_, but not _defined_, on platforms with
/// dynamic linking because the `SWT_NO_DYNAMIC_LINKING` C++ macro (not the
/// Swift compiler conditional of the same name) is not consistently declared
/// when Swift files import the `_TestingInternals` C++ module.
SWT_EXTERN const void *_Nonnull const SWTTypeMetadataSectionBounds[2];
SWT_EXTERN void swt_getStaticallyLinkedSectionBounds(size_t kind, const void *_Nullable *_Nonnull outSectionBegin, size_t *outByteCount);

#pragma mark - Legacy test discovery

Expand Down