Skip to content

Don't use swift_enumerateAllMetadataSections() on WASI. #730

New issue

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

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

Already on GitHub? # to your account

Merged
merged 3 commits into from
Sep 24, 2024
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
60 changes: 40 additions & 20 deletions Sources/_TestingInternals/Discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,24 +213,8 @@ struct SWTTypeMetadataRecord {
}
};

#if defined(SWT_NO_DYNAMIC_LINKING)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this bit down to clarify it's "Apple with static linking" rather than all static-linked cases. The code isn't changed.

#pragma mark - Statically-linked implementation

// This environment does not have a dynamic linker/loader. Therefore, there is
// only one image (this one) with Swift code in it.
// SEE: https://github.com/swiftlang/swift/tree/main/stdlib/public/runtime/ImageInspectionStatic.cpp

extern "C" const char sectionBegin __asm("section$start$__TEXT$__swift5_types");
extern "C" const char sectionEnd __asm("section$end$__TEXT$__swift5_types");

template <typename SectionEnumerator>
static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
auto size = std::distance(&sectionBegin, &sectionEnd);
bool stop = false;
body(nullptr, &sectionBegin, size, &stop);
}

#elif defined(__APPLE__)
#if defined(__APPLE__)
#if !defined(SWT_NO_DYNAMIC_LINKING)
#pragma mark - Apple implementation

/// A type that acts as a C++ [Container](https://en.cppreference.com/w/cpp/named_req/Container)
Expand Down Expand Up @@ -317,6 +301,24 @@ static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
}
}

#else
#pragma mark - Apple implementation (statically linked)

// This environment does not have a dynamic linker/loader. Therefore, there is
// only one image (this one) with Swift code in it.
// SEE: https://github.com/swiftlang/swift/tree/main/stdlib/public/runtime/ImageInspectionStatic.cpp

extern "C" const char sectionBegin __asm("section$start$__TEXT$__swift5_types");
extern "C" const char sectionEnd __asm("section$end$__TEXT$__swift5_types");

template <typename SectionEnumerator>
static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
auto size = std::distance(&sectionBegin, &sectionEnd);
bool stop = false;
body(nullptr, &sectionBegin, size, &stop);
}
#endif

#elif defined(_WIN32)
#pragma mark - Windows implementation

Expand Down Expand Up @@ -396,7 +398,7 @@ static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
// modules do not support unloading, so we'll just not worry about them.)
using SWTSectionList = SWTVector<std::tuple<HMODULE, const void *, size_t>>;
SWTSectionList sectionList;
for (DWORD i = 0; i < hModuleCount; i++) {
for (size_t i = 0; i < hModuleCount; i++) {
if (auto section = findSection(hModules[i], ".sw5tymd")) {
sectionList.emplace_back(hModules[i], section->first, section->second);
}
Expand All @@ -417,8 +419,26 @@ static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
}
}

#elif defined(__wasi__)
#pragma mark - WASI implementation (statically linked)

extern "C" const char __start_swift5_type_metadata;
extern "C" const char __stop_swift5_type_metadata;

template <typename SectionEnumerator>
static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
const auto& sectionBegin = __start_swift5_type_metadata;
const auto& sectionEnd = __stop_swift5_type_metadata;

// WASI only has a single image (so far) and it is statically linked, so all
// Swift metadata ends up in the same section bounded by the named symbols
// above. So we can just yield the section betwixt them.
auto size = std::distance(&sectionBegin, &sectionEnd);
bool stop = false;
body(nullptr, &sectionBegin, size, &stop);
}

#elif defined(__linux__) || defined(__FreeBSD__) || defined(__wasi__) || defined(__ANDROID__)
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__ANDROID__)
#pragma mark - ELF implementation

/// Specifies the address range corresponding to a section.
Expand Down