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

interpret: handle Box with non-ZST allocator #96198

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 20 additions & 5 deletions compiler/rustc_const_eval/src/interpret/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,10 @@ where
let layout = self.layout_of(pointee_type)?;
let (ptr, meta) = match **val {
Immediate::Scalar(ptr) => (ptr, MemPlaceMeta::None),
Immediate::ScalarPair(ptr, meta) => (ptr, MemPlaceMeta::Meta(meta.check_init()?)),
Immediate::ScalarPair(ptr, meta) => {
assert!(layout.is_unsized());
(ptr, MemPlaceMeta::Meta(meta.check_init()?))
}
};

let mplace = MemPlace {
Expand All @@ -298,8 +301,18 @@ where
&self,
src: &OpTy<'tcx, M::PointerTag>,
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
let val = self.read_immediate(src)?;
trace!("deref to {} on {:?}", val.layout.ty, *val);
// HACK we need to special-case dereferencing a `Box` with a non-ZST allocator.
// See https://github.com/rust-lang/rust/issues/95453; codegen does similar things.
let src = if src.layout.ty.is_box() && !src.layout.field(self, 1).is_zst() {
// Extract `Box<T>` -> `Unique<T>` -> `NonNull<T>` -> `*const T`
let uniq = self.operand_field(src, 0)?;
let nonnull = self.operand_field(&uniq, 0)?;
self.operand_field(&nonnull, 0)?
} else {
*src
};
let val = self.read_immediate(&src)?;
debug!("deref to {} on {:?}", val.layout.ty, *val);
let mplace = self.ref_to_mplace(&val)?;
self.check_mplace_access(mplace, CheckInAllocMsg::DerefTest)?;
Ok(mplace)
Expand Down Expand Up @@ -762,7 +775,8 @@ where
Abi::Scalar(_) => {} // fine
_ => span_bug!(
self.cur_span(),
"write_immediate_to_mplace: invalid Scalar layout: {:#?}",
"write_immediate_to_mplace: invalid layout for value {:?}: {:#?}",
value,
dest.layout
),
}
Expand All @@ -774,7 +788,8 @@ where
// which `ptr.offset(b_offset)` cannot possibly fail to satisfy.
let Abi::ScalarPair(a, b) = dest.layout.abi else { span_bug!(
self.cur_span(),
"write_immediate_to_mplace: invalid ScalarPair layout: {:#?}",
"write_immediate_to_mplace: invalid layout for value {:?}: {:#?}",
value,
dest.layout
)
};
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_const_eval/src/interpret/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,9 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
self.check_safe_pointer(value, "reference")?;
Ok(true)
}
ty::Adt(def, ..) if def.is_box() => {
// HACK: We only treat boxes with ZST allocators as primitives.
// See https://github.com/rust-lang/rust/issues/95453.
ty::Adt(def, ..) if def.is_box() && value.layout.field(self.ecx, 1).is_zst() => {
self.check_safe_pointer(value, "box")?;
Ok(true)
}
Expand Down Expand Up @@ -786,7 +788,10 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
return Ok(());
}
// Sanity check: `builtin_deref` does not know any pointers that are not primitive.
assert!(op.layout.ty.builtin_deref(true).is_none());
// FIXME: `builtin_deref` treats `Box` as a pointer even with arbitrary allocators,
// but we cannot treat that as a primitive, so we have to disable this.
// See https://github.com/rust-lang/rust/issues/95453.
// assert!(op.layout.ty.builtin_deref(true).is_none());

// Special check preventing `UnsafeCell` in the inner part of constants
if let Some(def) = op.layout.ty.ty_adt_def() {
Expand Down