Skip to content

Commit 0bc44f7

Browse files
committed
Move provenance checks out of interning method.
1 parent e163486 commit 0bc44f7

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

compiler/rustc_const_eval/src/interpret/intern.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ pub fn intern_const_alloc_recursive<
450450
Ok(())
451451
}
452452

453-
/// Intern `ret`, checking it references no other allocation.
453+
/// Intern `ret`. This function assumes that `ret` references no other allocation.
454454
#[instrument(level = "debug", skip(ecx))]
455455
pub fn intern_const_alloc_for_constprop<
456456
'mir,
@@ -461,17 +461,7 @@ pub fn intern_const_alloc_for_constprop<
461461
ecx: &mut InterpCx<'mir, 'tcx, M>,
462462
ret: &MPlaceTy<'tcx>,
463463
) -> InterpResult<'tcx, ()> {
464-
let Some((size, align)) = ecx.size_and_align_of_mplace(ret)? else {
465-
throw_inval!(ConstPropNonsense)
466-
};
467-
468-
let alloc_ref = ecx.get_ptr_alloc(ret.ptr(), size, align)?.unwrap();
469-
// Do not try interning a value that contains provenance.
470-
if alloc_ref.has_provenance() {
471-
throw_inval!(ConstPropNonsense)
472-
}
473-
474-
// remove allocation
464+
// Move allocation to `tcx`.
475465
let alloc_id = ret.ptr().provenance.unwrap();
476466
let Some((_, mut alloc)) = ecx.memory.alloc_map.remove(&alloc_id) else {
477467
// Pointer not found in local memory map. It is either a pointer to the global

compiler/rustc_const_eval/src/interpret/memory.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ impl<'tcx, 'a, Prov: Provenance, Extra, Bytes: AllocBytes> AllocRef<'a, 'tcx, Pr
10211021
}
10221022

10231023
/// Returns whether the allocation has provenance anywhere in the range of the `AllocRef`.
1024-
pub(crate) fn has_provenance(&self) -> bool {
1024+
pub fn has_provenance(&self) -> bool {
10251025
!self.alloc.provenance().range_empty(self.range, &self.tcx)
10261026
}
10271027
}

compiler/rustc_mir_transform/src/gvn.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,7 @@ fn op_to_prop_const<'tcx>(
825825
// If this constant has scalar ABI, return it as a `ConstValue::Scalar`.
826826
if let Abi::Scalar(abi::Scalar::Initialized { .. }) = op.layout.abi
827827
&& let Ok(scalar) = ecx.read_scalar(op)
828+
&& scalar.try_to_int().is_ok()
828829
{
829830
return Some(ConstValue::Scalar(scalar));
830831
}
@@ -833,6 +834,14 @@ fn op_to_prop_const<'tcx>(
833834
if let Either::Left(mplace) = op.as_mplace_or_imm()
834835
&& let MemPlaceMeta::None = mplace.meta()
835836
{
837+
let (size, align) = ecx.size_and_align_of_mplace(&mplace).ok()??;
838+
839+
let alloc_ref = ecx.get_ptr_alloc(mplace.ptr(), size, align).ok()??;
840+
// Do not try interning a value that contains provenance.
841+
if alloc_ref.has_provenance() {
842+
return None;
843+
}
844+
836845
intern_const_alloc_for_constprop(ecx, &mplace).ok()?;
837846
let pointer = mplace.ptr().into_pointer_or_addr().ok()?;
838847
let (alloc_id, offset) = pointer.into_parts();
@@ -846,7 +855,13 @@ fn op_to_prop_const<'tcx>(
846855
// Everything failed: create a new allocation to hold the data.
847856
let alloc_id =
848857
ecx.intern_with_temp_alloc(op.layout, |ecx, dest| ecx.copy_op(op, dest, false)).ok()?;
849-
Some(ConstValue::Indirect { alloc_id, offset: Size::ZERO })
858+
let value = ConstValue::Indirect { alloc_id, offset: Size::ZERO };
859+
860+
if !value.has_provenance(*ecx.tcx, op.layout.size) {
861+
return Some(value);
862+
}
863+
864+
None
850865
}
851866

852867
impl<'tcx> VnState<'_, 'tcx> {
@@ -898,9 +913,7 @@ impl<'tcx> VnState<'_, 'tcx> {
898913

899914
// Check that we do not leak a pointer.
900915
// Those pointers may lose part of their identity in codegen.
901-
if value.has_provenance(self.tcx, op.layout.size) {
902-
return None;
903-
}
916+
assert!(!value.has_provenance(self.tcx, op.layout.size));
904917

905918
let const_ = Const::Val(value, op.layout.ty);
906919
Some(ConstOperand { span: rustc_span::DUMMY_SP, user_ty: None, const_ })

0 commit comments

Comments
 (0)