Skip to content
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

[Closes #471] Use where bound to const check at compilation time #592

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions kernel-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ lfs = []
[profile.dev]
panic = "abort"
opt-level = 1
# TODO: The `const_generics` and `const_evaluatable_checked` feature causes an ICE on low nightly versions
# when trying incremental compilation. Remove this after upgrading to a higher nightly version.
incremental = false

[profile.release]
panic = "abort"
Expand Down
2 changes: 2 additions & 0 deletions kernel-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
#![feature(const_mut_refs)]
#![feature(const_precise_live_drops)]
#![feature(const_trait_impl)]
#![feature(const_generics)]
#![feature(const_evaluatable_checked)]
#![feature(generic_associated_types)]
#![feature(maybe_uninit_extra)]
#![feature(raw_ref_op)]
Expand Down
15 changes: 5 additions & 10 deletions kernel-rs/src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,11 @@ impl Page {
}
}

pub fn as_uninit_mut<T>(&mut self) -> &mut MaybeUninit<T> {
// TODO(https://github.com/kaist-cp/rv6/issues/471): Use const_assert! (or equivalent)
// instead. Currently, use of T inside const_assert! incurs a compile error: "can't use
// generic parameters from outer function". Also, there's a workaround using
// feature(const_generics) and feature(const_evaluatable_checked). However, using them makes
// the compiler panic. When the compiler becomes updated, we will fix the following lines to
// use static checks.
assert!(mem::size_of::<T>() <= PGSIZE);
assert_eq!(PGSIZE % mem::align_of::<T>(), 0);

pub fn as_uninit_mut<T>(&mut self) -> &mut MaybeUninit<T>
where
[(); PGSIZE - mem::size_of::<T>()]: , /* We need mem::size_of::<T>() <= PGSIZE */
[(); PGSIZE % mem::align_of::<T>() + usize::MAX]: , /* We need PGSIZE % mem::align_of::<T> == 0 */
{
// SAFETY: self.inner is an array of length PGSIZE aligned with PGSIZE bytes.
// The above assertions show that it can contain a value of T. As it contains arbitrary
// data, we cannot treat it as &mut T. Instead, we use &mut MaybeUninit<T>. It's ok because
Expand Down
9 changes: 5 additions & 4 deletions kernel-rs/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ pub fn memmove(dst: &mut [u8], src: &[u8]) {
/// # SAFETY
///
/// Filling a value of `T` with a value of `S` must not break the invariant of `T`.
pub unsafe fn memset<T, S: Copy>(dst: &mut T, v: S) {
// Cannot use `const_assert!` here. Compiler optimization will remove `assert!`.
assert!(core::mem::size_of::<T>() % core::mem::size_of::<S>() == 0);
assert!(core::mem::align_of::<T>() % core::mem::align_of::<S>() == 0);
pub unsafe fn memset<T, S: Copy>(dst: &mut T, v: S)
where
[(); core::mem::size_of::<T>() % core::mem::size_of::<S>() + usize::MAX]: , /* We need mem::size_of::<T>() % mem::size_of::<S>() == 0 */
[(); core::mem::align_of::<T>() % core::mem::align_of::<S>() + usize::MAX]: , /* We need mem::align_of::<T>() % mem::align_of::<S>() == 0 */
{
// SAFETY: T's size/alignment is a multiple of S's size/alignment.
let buf = unsafe {
core::slice::from_raw_parts_mut(
Expand Down