-
Notifications
You must be signed in to change notification settings - Fork 11
[safety] Weaken lifetime in get_xde_state() #1016
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
Open
daniel-levin
wants to merge
1
commit into
master
Choose a base branch
from
c3
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
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.
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 agree with this in principle, but I'm not sure that it helps in the worst case? I.e., any caller can still invoke
let state: &'static _ = get_xde_state();and get a static reference. If the compiler typically picks a narrower'awhen this isn't specified, that might suffice though.I'm otherwise not sure what the best way is to encode the idea that this pointer's validity does outlive the scope of almost every method within the module. I.e., when the module is not attached I'd expect everything else to be functionally inert.
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.
This is a good point... I think it may be possible to constrain the lifetime to non-static.
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.
this is effectively the same problem as having a reference to "library-local" data in a Rust-originated .so in userland (which is kind of an Unresolved Problem, lots of proposals for a "dynamic" lifetime etc to describe this, but none of it really has gone anywhere).
strictly speaking, returning
&'static XdeStateis not itself undefined behavior, but using it whilexde_dipis deallocated is. so a more narrow lifetime which still does not exactly track with the initialized-ness ofxde_dipcould still have the problem. realistically what would be "ideal" is to have some handle to the library provided as an argument to all the xde functions which you couldhandle.get_xde_state()to get an appropriately-scoped lifetime and know you're not leaking a reference ofxde_dipinto some external code where it will get used after the module might be unloaded. as long as you never unload xde the'staticis fine, even! (ish. I think we all assume that nothing else calls functions inxdewhile we're going throughxde_attach)the next best thing would probably be to have an
unsafe fn xde_handle() -> XdeHandlethat acts as a witness that the library is loaded (where you disclaim that the library is initialized when calling it). thenXdeHandle::get_xde_state()would be a more "normal"get_xde_state(&self) -> &XdeState, where the lifetime is tied to the ZST handle thingy so it can't get automagically expanded incorrectly. that ZST should probably not be copy or clone, since you wouldn't want to inadvertently leak those "witnesses" of the module around.finally you'd want to interlock
XdeHandleexisting anywhere andxde_detach(). if anyXdeHandlewould be living on detach that's a bug anyway. that looks like it is an existing bug: if some thread is inxde_ioc_iopte_command, nothing hasmanagement_lock, so you could go detach the module, null outxde_dip, and have the system die on the null deref right?how much of the use of
xde_dipis in hot paths, would it be reasonable to have a refcount as another static here, bumped onxde_handle()and decremented on drop? then you could guard detach on that?