Correctly handle interior mutable data in Box

Previously, the pointee type would be behind a `*const` pointer, so
`ty_is_freeze` would always be `true`, even if there was an
`UnsafeCell` in `Box`.
This commit is contained in:
Xinglu Chen 2025-04-30 10:22:15 +02:00
parent 023854875c
commit 8b3637635b
3 changed files with 26 additions and 1 deletions

View File

@ -173,7 +173,7 @@ impl<'tcx> NewPermission {
pointee.is_unpin(*cx.tcx, cx.typing_env()).then_some(()).map(|()| {
// Regular `Unpin` box, give it `noalias` but only a weak protector
// because it is valid to deallocate it within the function.
let ty_is_freeze = ty.is_freeze(*cx.tcx, cx.typing_env());
let ty_is_freeze = pointee.is_freeze(*cx.tcx, cx.typing_env());
let protected = kind == RetagKind::FnEntry;
let initial_state = Permission::new_reserved(ty_is_freeze, protected);
Self {

View File

@ -0,0 +1,19 @@
//@compile-flags: -Zmiri-tree-borrows
#![feature(box_as_ptr)]
#[path = "../../utils/mod.rs"]
#[macro_use]
mod utils;
use std::cell::UnsafeCell;
pub fn main() {
let cell = UnsafeCell::new(42);
let mut root = Box::new(cell);
let a = Box::as_mut_ptr(&mut root);
unsafe {
name!(a);
let alloc_id = alloc_id!(a);
print_state!(alloc_id);
}
}

View File

@ -0,0 +1,6 @@
──────────────────────────────────────────────────
Warning: this tree is indicative only. Some tags may have been hidden.
0.. 4
| Act | └─┬──<TAG=root of the allocation>
| ReIM| └────<TAG=a>
──────────────────────────────────────────────────