-
Notifications
You must be signed in to change notification settings - Fork 450
rust: kernel: add missing safety comments #382
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,13 +79,29 @@ pub enum SeekFrom { | |
Current(i64), | ||
} | ||
|
||
/// Called by the VFS when an inode should be opened. | ||
/// | ||
/// Calls `T::open` on the returned value of `A::convert`. | ||
/// | ||
/// # Safety | ||
/// | ||
/// The returned value of `A::convert` must be a valid non-null pointer and | ||
/// `T:open` must return a valid non-null pointer on an `Ok` result. | ||
unsafe extern "C" fn open_callback<A: FileOpenAdapter, T: FileOpener<A::Arg>>( | ||
inode: *mut bindings::inode, | ||
file: *mut bindings::file, | ||
) -> c_types::c_int { | ||
from_kernel_result! { | ||
// SAFETY: `A::convert` must return a valid non-null pointer that | ||
// should point to data in the inode or file that lives longer | ||
// than the following use of `T::open`. | ||
let arg = unsafe { A::convert(inode, file) }; | ||
// SAFETY: `arg` was previously returned by `A::convert` and must | ||
// be a valid non-null pointer. | ||
let ptr = T::open(unsafe { &*arg })?.into_pointer(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only the (note that I do not know the answer to that question myself, this is intended to show what questions to ask and answer) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it. I wrote it this way because I'm assuming the pointer returned by convert is going to point to something in the inode or file which will definitely live beyond open callback and will live beyond |
||
// SAFETY: `ptr` was previously returned by `T::open`. The returned | ||
// value should be a boxed value and should live the length of the | ||
// given file. | ||
unsafe { (*file).private_data = ptr as *mut c_types::c_void }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To arrive at a correct comment here, ask yourself how you can prove that it is always safe to dereference the raw pointer called (note that I do not know the answer to that question myself, this is intended to show what questions to ask and answer) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated this one a bit. I whiffed on the first draft. |
||
Ok(0) | ||
} | ||
|
@@ -500,7 +516,8 @@ pub trait FileOpenAdapter { | |
/// # Safety | ||
/// | ||
/// This function must be called only when [`struct file_operations::open`] is being called for | ||
/// a file that was registered by the implementer. | ||
/// a file that was registered by the implementer. The returned pointer must be valid and | ||
/// not-null. | ||
unsafe fn convert(_inode: *mut bindings::inode, _file: *mut bindings::file) | ||
-> *const Self::Arg; | ||
} | ||
|
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.
Was going to add comments and docs to all the helpers, but wasn't sure if this was something needed/wanted. If this is helpful, I can update with the rest documented.