-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Clean up PlatformDarwinKernel::GetSharedModule, document #78652
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
jasonmolenda
merged 2 commits into
llvm:main
from
jasonmolenda:clean-up-platformdarwinkernel-getsharedmodulekernel
Jan 19, 2024
+49
−43
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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.
I am sure that this is a dumb question, so forgive me ...
It seems like we can end up here with a "useless" pointer to an instance of
Module
inmodule_sp
. Fortunately, the first thing thatPlatformDarwin::GetSharedModule
does is to reset that pointer which means that there is no leak. However, would it make sense to do areset
before calling just in case the code in that function changes in the future?Again, I apologize if this is a stupid question!
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.
Not a dumb question at all!
module_sp
is an "out parameter" and not clearing an out parameter is a common opportunity for bugs. Whether it's a "bug" depends on the contract of course. Per the documentation that Jason added here ("A Module that matches the ModuleSpec, if one is found.") it should only be set when a module is found (and assuming it applies to all the overloads) the same expectation could be had aboutPlatformDarwin::GetSharedModule
.The obvious way to sidestep this issue is to not use out parameters at all. If the shared pointer was the only out parameter we could've potentially refactored this to return an
Expected<ModuleSP>
or something, but we have a handful of other out parameters that suffer from the same problem.Regardless, as we're using smart pointers, there's no risk of a leak. Therefore I don't think the extra reset is warranted. If we really wanted to be defensive, I'd prefer to use
asserts
to catch cases where we're not holding ourselves to these kind of contracts.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.
Of course! Now that I looked at the signature and saw that
module_sp
is passed by reference, we assume that it will go out of the caller's scope (or the caller's, caller's scope ...) at some point and the "leaked" instance ofModule
would be cleaned up then!Thank you for the explanation.
It just seemed odd to have that contain a pointer at all at that point in the code because the point of calling
PlatformDarwin::GetSharedModule
is that a valid one was not found. I.e., if the condition on lines 805/806 was false, it seemed to me that "A Module that matches the ModuleSpec" was not actually found and somodule_sp
should have been reset before either falling out of the loop or going to the next iteration.Thank you again for the explanation!
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 the question, yeah Jonas is right I shouldn't have bothered reseting the shared pointer at the beginning, I did that more to demonstrate in the code that it does not have a value on entry to the method, to make it clearer for the reader. An assert would work just as well.
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.
Sorry to follow up again, @jasonmolenda, but I think that putting an assert at the top of
PlatformDarwin::GetSharedModule
might have exposed the curiosity that I am thinking about ...Let's say that we are on the last iteration of this loop:
module_sp
isreset
to point to a pointer to a new instantiation of aModule
. Further, assume that(module_sp && module_sp->GetObjectFile() && module_sp->MatchesModuleSpec(kern_spec))
is not true. That means that we fall out of that loop and
module_sp
still points to an instance of aModule
. We proceed to the next loop and, for the sake of argument, let's say thatobjfile_names
contains 0 entries for everypossible_kernel_dsym
. Effectively, then, there will be no iterations of that second loop.As a result, control will pass to
PlatformDarwin::GetSharedModule
withmodule_sp
pointing to a leftover instance of aModule
from the last iteration of the first loop. It seems like it should be that we resetmodule_sp
before doing "the next" iteration on both of the loops in the function. I am sorry if it seems like I am being hard headed. I am just trying to understand and help!Thank you for your time!!