Add rustfmt from relibc and apply it with cargo fmt

This commit is contained in:
Jeremy Soller
2024-01-17 13:52:01 -07:00
parent 73897bd83d
commit 45f1c4e29e
166 changed files with 7353 additions and 3851 deletions
+79 -31
View File
@@ -1,9 +1,11 @@
use crate::paging::{Page, VirtualAddress};
use crate::memory::PAGE_SIZE;
use crate::{
memory::PAGE_SIZE,
paging::{Page, VirtualAddress},
};
use crate::arch::{arch_copy_to_user, arch_copy_from_user};
use crate::arch::{arch_copy_from_user, arch_copy_to_user};
use crate::syscall::error::{Error, EINVAL, EFAULT, Result};
use crate::syscall::error::{Error, Result, EFAULT, EINVAL};
#[derive(Clone, Copy)]
pub struct UserSlice<const READ: bool, const WRITE: bool> {
@@ -16,10 +18,7 @@ pub type UserSliceRw = UserSlice<true, true>;
impl<const READ: bool, const WRITE: bool> UserSlice<READ, WRITE> {
pub fn empty() -> Self {
Self {
base: 0,
len: 0,
}
Self { base: 0, len: 0 }
}
pub fn len(&self) -> usize {
self.len
@@ -42,7 +41,16 @@ impl<const READ: bool, const WRITE: bool> UserSlice<READ, WRITE> {
if idx > self.len {
return None;
}
Some((Self { base: self.base, len: idx }, Self { base: self.base + idx, len: self.len - idx }))
Some((
Self {
base: self.base,
len: idx,
},
Self {
base: self.base + idx,
len: self.len - idx,
},
))
}
pub fn advance(self, by: usize) -> Option<Self> {
Some(self.split_at(by)?.1)
@@ -51,25 +59,35 @@ impl<const READ: bool, const WRITE: bool> UserSlice<READ, WRITE> {
Some(self.split_at(to)?.0)
}
pub fn none_if_null(self) -> Option<Self> {
if self.addr() == 0 { None } else { Some(self) }
if self.addr() == 0 {
None
} else {
Some(self)
}
}
/// Not unsafe, because user memory is not covered by the memory model that decides if
/// something is UB, but it can break logic invariants
pub fn reinterpret_unchecked<const NEW_READ: bool, const NEW_WRITE: bool>(self) -> UserSlice<NEW_READ, NEW_WRITE> {
pub fn reinterpret_unchecked<const NEW_READ: bool, const NEW_WRITE: bool>(
self,
) -> UserSlice<NEW_READ, NEW_WRITE> {
UserSlice {
base: self.base,
len: self.len,
}
}
pub fn in_variable_chunks(self, chunk_size: usize) -> impl Iterator<Item = Self> {
(0..self.len()).step_by(chunk_size).map(move |i| self.advance(i).expect("already limited by length, must succeed"))
(0..self.len()).step_by(chunk_size).map(move |i| {
self.advance(i)
.expect("already limited by length, must succeed")
})
}
pub fn in_exact_chunks(self, chunk_size: usize) -> impl Iterator<Item = Self> {
(0..self.len().div_floor(chunk_size))
.map(move |i| {
self.advance(i * chunk_size).expect("already limited by length, must succeed")
.limit(chunk_size).expect("length is aligned")
})
(0..self.len().div_floor(chunk_size)).map(move |i| {
self.advance(i * chunk_size)
.expect("already limited by length, must succeed")
.limit(chunk_size)
.expect("length is aligned")
})
}
}
impl<const WRITE: bool> UserSlice<true, WRITE> {
@@ -88,35 +106,51 @@ impl<const WRITE: bool> UserSlice<true, WRITE> {
}
pub unsafe fn read_exact<T>(self) -> Result<T> {
let mut t: T = core::mem::zeroed();
let slice = unsafe { core::slice::from_raw_parts_mut((&mut t as *mut T).cast::<u8>(), core::mem::size_of::<T>()) };
let slice = unsafe {
core::slice::from_raw_parts_mut(
(&mut t as *mut T).cast::<u8>(),
core::mem::size_of::<T>(),
)
};
self.limit(core::mem::size_of::<T>()).ok_or(Error::new(EINVAL))?.copy_to_slice(slice)?;
self.limit(core::mem::size_of::<T>())
.ok_or(Error::new(EINVAL))?
.copy_to_slice(slice)?;
Ok(t)
}
pub fn copy_common_bytes_to_slice(self, slice: &mut [u8]) -> Result<usize> {
let min = core::cmp::min(self.len(), slice.len());
self.limit(min).expect("min(len, x) is always <= len").copy_to_slice(&mut slice[..min])?;
self.limit(min)
.expect("min(len, x) is always <= len")
.copy_to_slice(&mut slice[..min])?;
Ok(min)
}
// TODO: Merge int IO functions?
pub fn read_usize(self) -> Result<usize> {
let mut ret = 0_usize.to_ne_bytes();
self.limit(core::mem::size_of::<usize>()).ok_or(Error::new(EINVAL))?.copy_to_slice(&mut ret)?;
self.limit(core::mem::size_of::<usize>())
.ok_or(Error::new(EINVAL))?
.copy_to_slice(&mut ret)?;
Ok(usize::from_ne_bytes(ret))
}
pub fn read_u32(self) -> Result<u32> {
let mut ret = 0_u32.to_ne_bytes();
self.limit(4).ok_or(Error::new(EINVAL))?.copy_to_slice(&mut ret)?;
self.limit(4)
.ok_or(Error::new(EINVAL))?
.copy_to_slice(&mut ret)?;
Ok(u32::from_ne_bytes(ret))
}
pub fn read_u64(self) -> Result<u64> {
let mut ret = 0_u64.to_ne_bytes();
self.limit(8).ok_or(Error::new(EINVAL))?.copy_to_slice(&mut ret)?;
self.limit(8)
.ok_or(Error::new(EINVAL))?
.copy_to_slice(&mut ret)?;
Ok(u64::from_ne_bytes(ret))
}
pub fn usizes(self) -> impl Iterator<Item = Result<usize>> {
self.in_exact_chunks(core::mem::size_of::<usize>()).map(Self::read_usize)
self.in_exact_chunks(core::mem::size_of::<usize>())
.map(Self::read_usize)
}
}
impl<const READ: bool> UserSlice<READ, true> {
@@ -135,23 +169,33 @@ impl<const READ: bool> UserSlice<READ, true> {
}
pub fn copy_common_bytes_from_slice(self, slice: &[u8]) -> Result<usize> {
let min = core::cmp::min(self.len(), slice.len());
self.limit(min).expect("min(len, x) is always <= len").copy_from_slice(&slice[..min])?;
self.limit(min)
.expect("min(len, x) is always <= len")
.copy_from_slice(&slice[..min])?;
Ok(min)
}
pub fn copy_exactly(self, slice: &[u8]) -> Result<()> {
self.limit(slice.len()).ok_or(Error::new(EINVAL))?.copy_from_slice(slice)?;
self.limit(slice.len())
.ok_or(Error::new(EINVAL))?
.copy_from_slice(slice)?;
Ok(())
}
pub fn write_usize(self, word: usize) -> Result<()> {
self.limit(core::mem::size_of::<usize>()).ok_or(Error::new(EINVAL))?.copy_from_slice(&word.to_ne_bytes())?;
self.limit(core::mem::size_of::<usize>())
.ok_or(Error::new(EINVAL))?
.copy_from_slice(&word.to_ne_bytes())?;
Ok(())
}
pub fn write_u32(self, int: u32) -> Result<()> {
self.limit(core::mem::size_of::<u32>()).ok_or(Error::new(EINVAL))?.copy_from_slice(&int.to_ne_bytes())?;
self.limit(core::mem::size_of::<u32>())
.ok_or(Error::new(EINVAL))?
.copy_from_slice(&int.to_ne_bytes())?;
Ok(())
}
pub fn write_u64(self, int: u64) -> Result<()> {
self.limit(core::mem::size_of::<u64>()).ok_or(Error::new(EINVAL))?.copy_from_slice(&int.to_ne_bytes())?;
self.limit(core::mem::size_of::<u64>())
.ok_or(Error::new(EINVAL))?
.copy_from_slice(&int.to_ne_bytes())?;
Ok(())
}
}
@@ -173,7 +217,8 @@ impl UserSliceRw {
}
fn is_kernel_mem(slice: &[u8]) -> bool {
(slice.as_ptr() as usize) >= crate::USER_END_OFFSET && (slice.as_ptr() as usize).checked_add(slice.len()).is_some()
(slice.as_ptr() as usize) >= crate::USER_END_OFFSET
&& (slice.as_ptr() as usize).checked_add(slice.len()).is_some()
}
/// Convert `[addr, addr+size)` into `(page, page_count)`.
@@ -192,5 +237,8 @@ pub fn validate_region(address: usize, size: usize) -> Result<(Page, usize)> {
if address.saturating_add(size) > crate::USER_END_OFFSET {
return Err(Error::new(EFAULT));
}
Ok((Page::containing_address(VirtualAddress::new(address)), size / PAGE_SIZE))
Ok((
Page::containing_address(VirtualAddress::new(address)),
size / PAGE_SIZE,
))
}