From 6e32a165208242153af66a12cbe4448b05525f82 Mon Sep 17 00:00:00 2001 From: DrMeepster <19316085+DrMeepster@users.noreply.github.com> Date: Sun, 26 Jun 2022 13:58:37 -0700 Subject: [PATCH 1/4] fix box with custom allocator in miri --- compiler/rustc_const_eval/src/interpret/cast.rs | 16 ---------------- .../rustc_const_eval/src/interpret/validity.rs | 5 ++++- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_const_eval/src/interpret/cast.rs b/compiler/rustc_const_eval/src/interpret/cast.rs index fb484fba9fd06..076415b2d1b2f 100644 --- a/compiler/rustc_const_eval/src/interpret/cast.rs +++ b/compiler/rustc_const_eval/src/interpret/cast.rs @@ -366,22 +366,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { } (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => { assert_eq!(def_a, def_b); - if def_a.is_box() || def_b.is_box() { - if !def_a.is_box() || !def_b.is_box() { - span_bug!( - self.cur_span(), - "invalid unsizing between {:?} -> {:?}", - src.layout.ty, - cast_ty.ty - ); - } - return self.unsize_into_ptr( - src, - dest, - src.layout.ty.boxed_ty(), - cast_ty.ty.boxed_ty(), - ); - } // unsizing of generic struct with pointer fields // Example: `Arc` -> `Arc` diff --git a/compiler/rustc_const_eval/src/interpret/validity.rs b/compiler/rustc_const_eval/src/interpret/validity.rs index 630281bb09254..3809030b51560 100644 --- a/compiler/rustc_const_eval/src/interpret/validity.rs +++ b/compiler/rustc_const_eval/src/interpret/validity.rs @@ -594,7 +594,10 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' Ok(true) } ty::Adt(def, ..) if def.is_box() => { - self.check_safe_pointer(value, "box")?; + let unique = self.ecx.operand_field(value, 0)?; + let nonnull = self.ecx.operand_field(&unique, 0)?; + let ptr = self.ecx.operand_field(&nonnull, 0)?; + self.check_safe_pointer(&ptr, "box")?; Ok(true) } ty::FnPtr(_sig) => { From d317988505cade110cc8c9f1d03e6a98622f10bd Mon Sep 17 00:00:00 2001 From: DrMeepster <19316085+DrMeepster@users.noreply.github.com> Date: Sun, 26 Jun 2022 18:54:03 -0700 Subject: [PATCH 2/4] validate box's allocator --- compiler/rustc_const_eval/src/interpret/validity.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/compiler/rustc_const_eval/src/interpret/validity.rs b/compiler/rustc_const_eval/src/interpret/validity.rs index 3809030b51560..00e4472721f3f 100644 --- a/compiler/rustc_const_eval/src/interpret/validity.rs +++ b/compiler/rustc_const_eval/src/interpret/validity.rs @@ -598,6 +598,9 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' let nonnull = self.ecx.operand_field(&unique, 0)?; let ptr = self.ecx.operand_field(&nonnull, 0)?; self.check_safe_pointer(&ptr, "box")?; + + let allocator = self.ecx.operand_field(value, 1)?; + self.visit_field(value, 1, &allocator)?; Ok(true) } ty::FnPtr(_sig) => { From 9f9c31171888150912487d97dba19c6586007310 Mon Sep 17 00:00:00 2001 From: DrMeepster <19316085+DrMeepster@users.noreply.github.com> Date: Tue, 28 Jun 2022 02:19:52 -0700 Subject: [PATCH 3/4] Validate all fields of box instead of validating allocator specifically --- compiler/rustc_const_eval/src/interpret/validity.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_const_eval/src/interpret/validity.rs b/compiler/rustc_const_eval/src/interpret/validity.rs index 00e4472721f3f..702954884a08b 100644 --- a/compiler/rustc_const_eval/src/interpret/validity.rs +++ b/compiler/rustc_const_eval/src/interpret/validity.rs @@ -599,8 +599,8 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' let ptr = self.ecx.operand_field(&nonnull, 0)?; self.check_safe_pointer(&ptr, "box")?; - let allocator = self.ecx.operand_field(value, 1)?; - self.visit_field(value, 1, &allocator)?; + // Check other fields of Box + self.walk_value(op)?; Ok(true) } ty::FnPtr(_sig) => { From 9039265c308fccf6599080db51e6db103ecaf5ad Mon Sep 17 00:00:00 2001 From: DrMeepster <19316085+DrMeepster@users.noreply.github.com> Date: Tue, 28 Jun 2022 13:48:13 -0700 Subject: [PATCH 4/4] fix silly mistake you should always run x.py check before pushing --- compiler/rustc_const_eval/src/interpret/validity.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_const_eval/src/interpret/validity.rs b/compiler/rustc_const_eval/src/interpret/validity.rs index 702954884a08b..905ab6cb578fc 100644 --- a/compiler/rustc_const_eval/src/interpret/validity.rs +++ b/compiler/rustc_const_eval/src/interpret/validity.rs @@ -600,7 +600,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' self.check_safe_pointer(&ptr, "box")?; // Check other fields of Box - self.walk_value(op)?; + self.walk_value(value)?; Ok(true) } ty::FnPtr(_sig) => {