Skip to content

Add exported symbol on non-Apple platforms to enumerate MetadataSection structures at runtime. #41621

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
Mar 5, 2022

Conversation

grynspan
Copy link
Contributor

@grynspan grynspan commented Mar 2, 2022

Add exported symbol on non-Apple platforms to enumerate MetadataSection structures at runtime.

On Apple platforms, it is possible to enumerate the loaded metadata section structures of a process using dyld API. This is useful when, for example, looking up types that conform to a specific protocol (as might be done by e.g. swift-argument-parser.)

On non-Apple platforms, it is not possible to look up these structures because their addresses are not exported. The structures are instead statically declared and, when an image is loaded, are ingested by swift_addNewDSOImage() (by way of static constructors emitted during compilation.)

The end result is that there is no way, even when leveraging deep knowledge of the Swift ABI, to examine loaded Swift metadata on non-Apple platforms. This PR adds an exported function to the runtime named swift_copyAllMetadataSections(). This function returns a C array of swift::MetadataSections structures. Client code can then call this function and inspect the resulting structures as needed.

@grynspan
Copy link
Contributor Author

grynspan commented Mar 2, 2022

@swift-ci please smoke test

@grynspan
Copy link
Contributor Author

grynspan commented Mar 2, 2022

@swift-ci please smoke test

@grynspan grynspan requested a review from kubamracek March 2, 2022 18:59
@grynspan grynspan force-pushed the jgrynspan/copy-metadata-sections branch 2 times, most recently from 2f42ef5 to b7862c9 Compare March 2, 2022 19:22
@grynspan
Copy link
Contributor Author

grynspan commented Mar 2, 2022

@swift-ci please test

@Azoy
Copy link
Contributor

Azoy commented Mar 2, 2022

This is tremendously useful, thank you!

@grynspan grynspan force-pushed the jgrynspan/copy-metadata-sections branch 2 times, most recently from f8428e0 to c1396c5 Compare March 2, 2022 21:28
@grynspan
Copy link
Contributor Author

grynspan commented Mar 2, 2022

Changed the function from copy to enumerate semantics:

  1. This avoids allocating a potentially large buffer, and
  2. The size of the structures is not guaranteed to be stable over time, so different sections structures might have different sizes. Clients can rely on the version field, but if it's a C array then bad things will happen if the client is out of date.

@grynspan
Copy link
Contributor Author

grynspan commented Mar 2, 2022

@swift-ci please test

@grynspan grynspan force-pushed the jgrynspan/copy-metadata-sections branch from c1396c5 to 4a43a08 Compare March 2, 2022 21:38
@grynspan
Copy link
Contributor Author

grynspan commented Mar 2, 2022

@swift-ci please smoke test

@grynspan
Copy link
Contributor Author

grynspan commented Mar 3, 2022

@swift-ci please smoke test linux

@grynspan
Copy link
Contributor Author

grynspan commented Mar 3, 2022

@swift-ci please test

1 similar comment
@grynspan
Copy link
Contributor Author

grynspan commented Mar 3, 2022

@swift-ci please test

@grynspan grynspan force-pushed the jgrynspan/copy-metadata-sections branch from 4df3d18 to 8648091 Compare March 3, 2022 17:27
@grynspan
Copy link
Contributor Author

grynspan commented Mar 3, 2022

@swift-ci please test

@grynspan grynspan force-pushed the jgrynspan/copy-metadata-sections branch from 8648091 to 49c3750 Compare March 3, 2022 17:58
@grynspan
Copy link
Contributor Author

grynspan commented Mar 3, 2022

@swift-ci please test

@grynspan grynspan force-pushed the jgrynspan/copy-metadata-sections branch from 49c3750 to 20b2166 Compare March 3, 2022 18:30
@grynspan
Copy link
Contributor Author

grynspan commented Mar 3, 2022

@swift-ci please test

@grynspan grynspan force-pushed the jgrynspan/copy-metadata-sections branch from 20b2166 to 6352e14 Compare March 3, 2022 18:57
@grynspan
Copy link
Contributor Author

grynspan commented Mar 3, 2022

@swift-ci please test

@grynspan grynspan force-pushed the jgrynspan/copy-metadata-sections branch from 6352e14 to 845904d Compare March 3, 2022 19:38
@grynspan
Copy link
Contributor Author

grynspan commented Mar 3, 2022

@swift-ci please test

@grynspan grynspan force-pushed the jgrynspan/copy-metadata-sections branch from 845904d to cce4d06 Compare March 3, 2022 20:00
@grynspan
Copy link
Contributor Author

grynspan commented Mar 3, 2022

@swift-ci please smoke test

@grynspan grynspan force-pushed the jgrynspan/copy-metadata-sections branch from cce4d06 to ec4c8e2 Compare March 3, 2022 20:56
@grynspan
Copy link
Contributor Author

grynspan commented Mar 3, 2022

@swift-ci please smoke test

@grynspan grynspan force-pushed the jgrynspan/copy-metadata-sections branch from ec4c8e2 to 1dc8b69 Compare March 3, 2022 21:29
@grynspan
Copy link
Contributor Author

grynspan commented Mar 3, 2022

@swift-ci please test

@grynspan grynspan force-pushed the jgrynspan/copy-metadata-sections branch 2 times, most recently from ff1806b to 0a87e61 Compare March 4, 2022 00:57
@grynspan
Copy link
Contributor Author

grynspan commented Mar 4, 2022

@swift-ci please test

@grynspan grynspan force-pushed the jgrynspan/copy-metadata-sections branch from 0a87e61 to bf1a309 Compare March 4, 2022 03:05
@grynspan
Copy link
Contributor Author

grynspan commented Mar 4, 2022

Boy, <atomic> is giving me a headache today.

@grynspan
Copy link
Contributor Author

grynspan commented Mar 4, 2022

@swift-ci please test

@grynspan grynspan force-pushed the jgrynspan/copy-metadata-sections branch from bf1a309 to a938de3 Compare March 4, 2022 04:15
@grynspan
Copy link
Contributor Author

grynspan commented Mar 4, 2022

@swift-ci please test

@grynspan
Copy link
Contributor Author

grynspan commented Mar 4, 2022

@swift-ci please test macOS

@grynspan grynspan force-pushed the jgrynspan/copy-metadata-sections branch 2 times, most recently from b8019a7 to 86e7107 Compare March 4, 2022 14:50
@grynspan
Copy link
Contributor Author

grynspan commented Mar 4, 2022

@swift-ci please test

@grynspan grynspan force-pushed the jgrynspan/copy-metadata-sections branch 2 times, most recently from 65ec9a6 to beb0ef0 Compare March 4, 2022 17:34
@grynspan
Copy link
Contributor Author

grynspan commented Mar 4, 2022

@swift-ci please test

@grynspan grynspan force-pushed the jgrynspan/copy-metadata-sections branch from beb0ef0 to a93a760 Compare March 4, 2022 21:38
@grynspan
Copy link
Contributor Author

grynspan commented Mar 4, 2022

@swift-ci please test

Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

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

I suppose that we can deal with the memory allocation paths in a follow up (but we really should - that can be a difficult thing to isolate later).

@grynspan grynspan force-pushed the jgrynspan/copy-metadata-sections branch from a93a760 to 8451822 Compare March 5, 2022 04:47
@grynspan
Copy link
Contributor Author

grynspan commented Mar 5, 2022

@swift-ci please smoke test

@grynspan grynspan merged commit 597d709 into swiftlang:main Mar 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants