-
Notifications
You must be signed in to change notification settings - Fork 184
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
consolidate: fix a Miri error #394
consolidate: fix a Miri error #394
Conversation
This looks good to me. Would you be willing to bundle in the same changes to |
The same failure also happens when running this repo's tests with
As Frank noted, this also happens in
|
Prior to this commit, Miri would produce the following error when executed on the code of `consolidate_updates_slice`: ``` error: Undefined Behavior: attempting a read access using <3403> at alloc1431[0x8], but that tag does not exist in the borrow stack for this location --> src/main.rs:12:16 | 12 | if (*ptr1).0 == (*ptr2).0 && (*ptr1).1 == (*ptr2).1 { | ^^^^^^^^^ | | | attempting a read access using <3403> at alloc1431[0x8], but that tag does not exist in the borrow stack for this location | this error occurs as part of an access at alloc1431[0x8..0xc] | = help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental = help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information help: <3403> was created by a SharedReadWrite retag at offsets [0x0..0x48] --> src/main.rs:9:24 | 9 | let ptr1 = slice.as_mut_ptr().offset(offset as isize); | ^^^^^^^^^^^^^^^^^^ help: <3403> was later invalidated at offsets [0x0..0x48] by a Unique function-entry retag inside this call --> src/main.rs:10:24 | 10 | let ptr2 = slice.as_mut_ptr().offset(index as isize); | ^^^^^^^^^^^^^^^^^^ = note: BACKTRACE (of the first span): = note: inside `consolidate_updates_slice` at src/main.rs:12:16: 12:25 note: inside `main` --> src/main.rs:34:5 | 34 | consolidate_updates_slice(&mut v); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ``` The same is true for `consolidate_slice`. The warning is fixed by making sure that `slice.get_mut_ptr()` is only invoked a single time. It seems like calling `get_mut_ptr` on a slice invalidates all existing pointers to the slice. My guess is that this is because `get_mut_ptr` takes a `&mut self` and could therefore in principle swap/replace/truncate the slice buffer, which could make existing pointers dangle. `get_mut_ptr` doesn't do that but Rust cannot know based on the method signature only.
6e10ae0
to
ca75967
Compare
Of course! It's fixed now. |
Alternatively, we should explore what the performance cost of not using |
Prior to this change, Miri would produce the following error when executed on the code of
consolidate_updates_slice
:The warning is fixed by making sure that
slice.get_mut_ptr()
is only invoked a single time. It seems like callingget_mut_ptr
on a slice invalidates all existing pointers to the slice. My guess is that this is becauseget_mut_ptr
takes a&mut self
and could therefore in principle swap/replace/truncate the slice buffer, which could make existing pointers dangle.get_mut_ptr
doesn't do that but Rust cannot know based on the method signature only.Fixes https://github.com/MaterializeInc/database-issues/issues/5957.