Ensure nested allocations in statics do not get deduplicated

This commit is contained in:
Oli Scherer 2024-02-26 18:03:06 +00:00
parent 92414ab25d
commit d3514a036d
21 changed files with 259 additions and 54 deletions

View file

@ -457,16 +457,6 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
// Special handling for pointers to statics (irrespective of their type).
assert!(!self.ecx.tcx.is_thread_local_static(did));
assert!(self.ecx.tcx.is_static(did));
let is_mut = matches!(
self.ecx.tcx.def_kind(did),
DefKind::Static { mt: Mutability::Mut, .. }
) || !self
.ecx
.tcx
.type_of(did)
.no_bound_vars()
.expect("statics should not have generic parameters")
.is_freeze(*self.ecx.tcx, ty::ParamEnv::reveal_all());
// Mode-specific checks
match self.ctfe_mode {
Some(
@ -491,8 +481,26 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
}
None => {}
}
// Return alloc mutability
if is_mut { Mutability::Mut } else { Mutability::Not }
// Return alloc mutability. For "root" statics we look at the type to account for interior
// mutability; for nested statics we have no type and directly use the annotated mutability.
match self.ecx.tcx.def_kind(did) {
DefKind::Static { mt: Mutability::Mut, .. } => Mutability::Mut,
DefKind::Static { mt: Mutability::Not, nested: true } => {
Mutability::Not
}
DefKind::Static { mt: Mutability::Not, nested: false }
if !self
.ecx
.tcx
.type_of(did)
.no_bound_vars()
.expect("statics should not have generic parameters")
.is_freeze(*self.ecx.tcx, ty::ParamEnv::reveal_all()) =>
{
Mutability::Mut
}
_ => Mutability::Not,
}
}
GlobalAlloc::Memory(alloc) => alloc.inner().mutability,
GlobalAlloc::Function(..) | GlobalAlloc::VTable(..) => {