Clarify some safety conditions of Vm{Reader,Writer}

This commit is contained in:
Ruihan Li
2024-08-16 11:35:06 +08:00
committed by Tate, Hongliang Tian
parent fda0fa0512
commit 562e644375
4 changed files with 102 additions and 87 deletions

View File

@ -112,19 +112,19 @@ impl HasPaddr for Frame {
impl<'a> Frame {
/// Returns a reader to read data from it.
pub fn reader(&'a self) -> VmReader<'a> {
// SAFETY: the memory of the page is untyped, contiguous and is valid during `'a`.
// Currently, only slice can generate `VmWriter` with typed memory, and this `Frame` cannot
// generate or be generated from an alias slice, so the reader will not overlap with `VmWriter`
// with typed memory.
// SAFETY:
// - The memory range points to untyped memory.
// - The frame is alive during the lifetime `'a`.
// - Using `VmReader` and `VmWriter` is the only way to access the frame.
unsafe { VmReader::from_kernel_space(self.as_ptr(), self.size()) }
}
/// Returns a writer to write data into it.
pub fn writer(&'a self) -> VmWriter<'a> {
// SAFETY: the memory of the page is untyped, contiguous and is valid during `'a`.
// Currently, only slice can generate `VmReader` with typed memory, and this `Frame` cannot
// generate or be generated from an alias slice, so the writer will not overlap with `VmReader`
// with typed memory.
// SAFETY:
// - The memory range points to untyped memory.
// - The frame is alive during the lifetime `'a`.
// - Using `VmReader` and `VmWriter` is the only way to access the frame.
unsafe { VmWriter::from_kernel_space(self.as_mut_ptr(), self.size()) }
}
}