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

Fix oob get_unchecked, overlapping _nonoverlapping #21

Merged
merged 1 commit into from
Jun 19, 2022
Merged
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
18 changes: 18 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ jobs:
with:
command: test

miri:
name: Miri
runs-on: ubuntu-latest
env:
MIRIFLAGS: -Zmiri-tag-raw-pointers
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
components: miri
override: true
- uses: actions-rs/cargo@v1
with:
command: miri
args: test

fmt:
name: Rustfmt
runs-on: ubuntu-latest
Expand Down
15 changes: 9 additions & 6 deletions src/dmsort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl<'a, T> Drop for DmSorter<'a, T> {
// This code will only run on stack-unwind (panic).

// Move back all elements into the slice:
ptr::copy_nonoverlapping(self.dropped.as_ptr(), &mut self.slice[self.write], self.dropped.len());
ptr::copy_nonoverlapping(self.dropped.as_ptr(), self.slice.as_mut_ptr().add(self.write), self.dropped.len());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, the pointer created here is a pointer to a single element, but this call actually needs a pointer which has provenance over a range. This could be created with something like &mut self.slice[self.write..][..self.dropped.len()] as *mut T but just offsetting a pointer to the whole slice is simpler.


// Make sure the objects aren't destroyed when self.dropped is dropped (avoid-double-free).
self.dropped.set_len(0);
Expand All @@ -217,13 +217,14 @@ impl<'a, T> Drop for DmSorter<'a, T> {
unsafe fn unsafe_push<T>(vec: &mut Vec<T>, value: &T) {
let old_len = vec.len();
vec.reserve(1);
ptr::copy_nonoverlapping(value, vec.get_unchecked_mut(old_len), 1);
ptr::copy_nonoverlapping(value, vec.as_mut_ptr().add(old_len), 1);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This get_unchecked_mut is always out-of-bounds by definition. If you can tolerate a recent Rust version, this could also be implemented with &mut vec.spare_capacity_mut()[0], though I think pointer offsetting alone is totally fine here.

vec.set_len(old_len + 1);
}

#[inline(always)]
unsafe fn unsafe_copy<T>(slice: &mut [T], source: usize, dest: usize) {
ptr::copy_nonoverlapping(slice.get_unchecked(source), slice.get_unchecked_mut(dest), 1);
let ptr = slice.as_mut_ptr();
ptr::copy_nonoverlapping(ptr.add(source), ptr.add(dest), 1);
Comment on lines -226 to +227
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is only required with -Zmiri-tag-raw-pointers. But also, this could be implemented with slice::swap_unchecked if that were stabilized.

}

fn sort_move_by<T, F>(slice: &mut [T], mut compare: F)
Expand Down Expand Up @@ -266,7 +267,9 @@ where
|| compare(s.slice.get_unchecked(read), s.slice.get_unchecked(s.write - 1)) != Ordering::Less
{
// The element is order - keep it:
unsafe_copy(s.slice, read, s.write);
if read != s.write {
unsafe_copy(s.slice, read, s.write);
}
Comment on lines +270 to +272
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the calls here to unsafe_copy pass 0 for both the read and write index, making this an overlapping copy.

read += 1;
s.write += 1;
num_dropped_in_row = 0;
Expand Down Expand Up @@ -317,8 +320,8 @@ where
let old_len = s.dropped.len();
s.dropped.reserve(num_backtracked);
ptr::copy_nonoverlapping(
s.slice.get_unchecked(s.write),
s.dropped.get_unchecked_mut(old_len),
s.slice.as_ptr().add(s.write),
s.dropped.as_mut_ptr().add(old_len),
Comment on lines -320 to +324
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both of these indexes are out-of-bounds for the slice, but inbounds for the backing allocation.

num_backtracked,
);
s.dropped.set_len(old_len + num_backtracked);
Expand Down