-
Notifications
You must be signed in to change notification settings - Fork 207
Mark Bundle(_dsoHandle:) call in macro expansion with unsafe
#1462
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for posting this! We should definitely come up with a solution here that resolves warnings caused when using
#bundlewith strict memory safety enabled.That being said, I think instead of annotating this expression as
unsafe, it might be better to annotateBundle(_dsoHandle:)as@safeinstead.Bundle(_dsoHandle:)is always safe - we never try to dereference the pointer, we just lookup what binary contains this pointer which doesn't come with any memory unsafety (and we gracefully handle cases where it's not a pointer to any binary). MarkingBundle(_dsoHandle:)as@safewould eliminate the need forunsafehere and would signify that this API isn't actually unsafe. What do you think about that approach?side note:
Bundle(_dsoHandle:)isn't actually in this repo sinceBundleisn't in swift-foundation yet, so changing that wouldn't be a change here but we can still make that happen if it's the right approach.cc @Tantalum73 in case you have any thoughts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your reply! I didn't found the declaration of
Bundle.init(_dsoHandle)in any repo so I think it might not be open-sourced yet. Thanks to the@_alwaysEmitIntoClientattribute on it, I found this from its swift interface:I can't continue to inspect how
init(__dsoHandle:)works. However, in theelsecode block of#availableexpression,unsafeBitCast(_:to:)is called, which is unsafe. So I thinkBundle(_dsoHandle:)is also unsafe.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this
unsafeBitCastis more unsafe than getting and operating on the pointer itself, to be honest. This being said, I am not convinced that we should apply the@safeattribute toBundle.init(__dsoHandle)at this time. I think it's prudent to use theunsafemarker as introduced in this PR, as it means there is no rush to audit thisBundleinitializer. If there were a later decision to mark theBundleinitializer as safe in the SDK, we can then remove the marker from the macro.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks all yeah after thinking longer on it I agree with @glessard - lets go with adding this
unsafefor now since while the underlying calls that Foundation makes (effectivelydladdr) should be safe, it's not a formal guarantee and I also haven't evaluated if that'd be the case on other platforms too.@WindowsMEMZ have you tried running the unit tests against this change? I suspect that the macro unit test for
#bundleneeds to be updated to include thisunsafekeyword in the expected expansion for it to pass (but if it did pass for you we can go ahead and kick off CI to confirm)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing it out. I've add
unsafeto expected expansion in the latest commit.