Skip to content

Commit fc9c59b

Browse files
authored
Merge pull request #4580 from JoJoDeveloping/fix-4579-protector-0sized
Fix #4579 by checking if the strong protector is actually "active".
2 parents d1d6185 + c70557d commit fc9c59b

File tree

5 files changed

+12
-65
lines changed

5 files changed

+12
-65
lines changed

src/borrow_tracker/tree_borrows/tree.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,8 @@ impl<'tcx> Tree {
756756
// Don't check for protector if it is a Cell (see `unsafe_cell_deallocate` in `interior_mutability.rs`).
757757
// Related to https://github.com/rust-lang/rust/issues/55005.
758758
&& !perm.permission().is_cell()
759+
// Only trigger UB if the accessed bit is set, i.e. if the protector is actually protecting this offset. See #4579.
760+
&& perm.is_accessed()
759761
{
760762
Err(TransitionError::ProtectedDealloc)
761763
} else {

tests/fail/both_borrows/zero-sized-protected.rs

Lines changed: 0 additions & 18 deletions
This file was deleted.

tests/fail/both_borrows/zero-sized-protected.stack.stderr

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/fail/both_borrows/zero-sized-protected.tree.stderr

Lines changed: 0 additions & 32 deletions
This file was deleted.

tests/pass/both_borrows/basic_aliasing_model.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@revisions: stack tree
22
//@[tree]compile-flags: -Zmiri-tree-borrows
33
#![feature(allocator_api)]
4+
use std::alloc::{Layout, alloc, dealloc};
45
use std::cell::Cell;
56
use std::ptr;
67

@@ -305,5 +306,14 @@ fn zst() {
305306
let ptr = &raw mut *b as *mut ();
306307
drop(b);
307308
let _ref = &mut *ptr;
309+
310+
// zero-sized protectors do not affect deallocation
311+
fn with_protector(_x: &mut (), ptr: *mut u8, l: Layout) {
312+
// `_x` here is strongly protected but covers zero bytes.
313+
unsafe { dealloc(ptr, l) };
314+
}
315+
let l = Layout::from_size_align(1, 1).unwrap();
316+
let ptr = alloc(l);
317+
with_protector(&mut *ptr.cast::<()>(), ptr, l);
308318
}
309319
}

0 commit comments

Comments
 (0)