Skip to content

Unchecked page access #887

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
15 changes: 6 additions & 9 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl Table {
#[inline]
pub fn ingredient_index(&self, id: Id) -> IngredientIndex {
let (page_idx, _) = split_id(id);
self.pages[page_idx.0].ingredient
unsafe { self.pages.get_unchecked(page_idx.0).ingredient }
}

/// Get a reference to the data for `id`, which must have been allocated from this table with type `T`.
Expand All @@ -178,7 +178,7 @@ impl Table {
pub(crate) fn get<T: Slot>(&self, id: Id) -> &T {
let (page, slot) = split_id(id);
let page_ref = self.page::<T>(page);
&page_ref.data()[slot.0]
unsafe { page_ref.data().get_unchecked(slot.0) }
}

/// Get a raw pointer to the data for `id`, which must have been allocated from this table.
Expand All @@ -193,7 +193,7 @@ impl Table {
pub(crate) fn get_raw<T: Slot>(&self, id: Id) -> *mut T {
let (page, slot) = split_id(id);
let page_ref = self.page::<T>(page);
page_ref.page_data()[slot.0].get().cast::<T>()
unsafe { page_ref.page_data().get_unchecked(slot.0).get().cast::<T>() }
}

/// Gets a reference to the page which has slots of type `T`
Expand All @@ -203,7 +203,7 @@ impl Table {
/// If `page` is out of bounds or the type `T` is incorrect.
#[inline]
pub(crate) fn page<T: Slot>(&self, page: PageIndex) -> PageView<'_, T> {
self.pages[page.0].assert_type::<T>()
unsafe { self.pages.get_unchecked(page.0).assert_type::<T>() }
}

/// Allocate a new page for the given ingredient and with slots of type `T`
Expand All @@ -228,7 +228,7 @@ impl Table {
current_revision: Revision,
) -> MemoTableWithTypes<'_> {
let (page, slot) = split_id(id);
let page = &self.pages[page.0];
let page = unsafe { self.pages.get_unchecked(page.0) };
// SAFETY: We supply a proper slot pointer and the caller is required to pass the `current_revision`.
let memos = unsafe { &*(page.slot_vtable.memos)(page.get(slot), current_revision) };
// SAFETY: The `Page` keeps the correct memo types.
Expand All @@ -239,10 +239,7 @@ impl Table {
pub(crate) fn memos_mut(&mut self, id: Id) -> MemoTableWithTypesMut<'_> {
let (page, slot) = split_id(id);
let page_index = page.0;
let page = self
.pages
.get_mut(page_index)
.unwrap_or_else(|| panic!("index `{page_index}` is uninitialized"));
let page = unsafe { self.pages.get_unchecked_mut(page_index) };
// SAFETY: We supply a proper slot pointer and the caller is required to pass the `current_revision`.
let memos = unsafe { &mut *(page.slot_vtable.memos_mut)(page.get(slot)) };
// SAFETY: The `Page` keeps the correct memo types.
Expand Down
20 changes: 4 additions & 16 deletions src/zalsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,7 @@ impl Zalsa {
#[inline]
pub(crate) fn lookup_ingredient(&self, index: IngredientIndex) -> &dyn Ingredient {
let index = index.as_u32() as usize;
self.ingredients_vec
.get(index)
.unwrap_or_else(|| panic!("index `{index}` is uninitialized"))
.as_ref()
unsafe { self.ingredients_vec.get_unchecked(index).as_ref() }
}

pub(crate) fn ingredient_index_for_memo(
Expand Down Expand Up @@ -346,10 +343,7 @@ impl Zalsa {
index: IngredientIndex,
) -> (&mut dyn Ingredient, &mut Runtime) {
let index = index.as_u32() as usize;
let ingredient = self
.ingredients_vec
.get_mut(index)
.unwrap_or_else(|| panic!("index `{index}` is uninitialized"));
let ingredient = unsafe { self.ingredients_vec.get_unchecked_mut(index) };
(ingredient.as_mut(), &mut self.runtime)
}

Expand All @@ -376,11 +370,7 @@ impl Zalsa {

for (_, index) in self.ingredients_requiring_reset.iter() {
let index = index.as_u32() as usize;
let ingredient = self
.ingredients_vec
.get_mut(index)
.unwrap_or_else(|| panic!("index `{index}` is uninitialized"));

let ingredient = unsafe { self.ingredients_vec.get_unchecked_mut(index) };
ingredient.reset_for_new_revision(self.runtime.table_mut());
}

Expand All @@ -393,9 +383,7 @@ impl Zalsa {
let _span = tracing::debug_span!("evict_lru").entered();
for (_, index) in self.ingredients_requiring_reset.iter() {
let index = index.as_u32() as usize;
self.ingredients_vec
.get_mut(index)
.unwrap_or_else(|| panic!("index `{index}` is uninitialized"))
unsafe { self.ingredients_vec.get_unchecked_mut(index) }
.reset_for_new_revision(self.runtime.table_mut());
}
}
Expand Down
Loading