Skip to content

Commit 4b4b2a8

Browse files
authored
Merge pull request #1568 from N1ark/consts-rework-2
Don’t crash on provenance-less wide pointers
2 parents 330e81c + 8675c61 commit 4b4b2a8

4 files changed

Lines changed: 76 additions & 30 deletions

File tree

‎charon/rustc_const_uneval/src/pointer.rs‎

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,11 @@ impl<'tcx> ConstReader<'tcx> {
2323
op: &OpTy<'tcx>,
2424
) -> InterpResult<'tcx, Const<'tcx>> {
2525
let tcx = self.tcx;
26-
// Make sure we only read through if it's not dangling!
27-
let pointee = ecx.deref_pointer(op).discard_err().and_then(|place| {
28-
let (alloc_id, offset, _) = ecx.ptr_get_alloc_id(place.ptr(), 0).discard_err()?;
29-
Some((place, alloc_id, offset))
30-
});
31-
let Some((place, alloc_id, offset)) = pointee else {
32-
// Invalid pointer; try reading it as a raw address
33-
let int = ecx.read_scalar(op)?.to_scalar_int()?;
34-
let kind = ConstKind::PtrNoProvenance(int.to_uint(int.size()));
35-
return interp_ok(Const {
36-
ty: op.layout.ty,
37-
kind,
38-
});
26+
let ptr_ty = op.layout.ty;
27+
let (ty::Ref(.., mutability) | ty::RawPtr(_, mutability)) = *ptr_ty.kind() else {
28+
unreachable!("not a pointer type: {ptr_ty:?}")
3929
};
40-
// A pointer into the allocation of a `TypeId` can only be expressed as bytes.
41-
if let AllocTarget::TypeId(_) = self.alloc_target(alloc_id) {
42-
let kind = ConstKind::Memory(self.read_raw_bytes(ecx, op)?);
43-
return interp_ok(Const {
44-
ty: op.layout.ty,
45-
kind,
46-
});
47-
}
48-
30+
let place = ecx.deref_pointer(op)?;
4931
let ty = place.layout.ty;
5032
let unsize = if place.meta().has_meta() {
5133
let tail = tcx.struct_tail_for_codegen(ty, self.typing_env);
@@ -60,6 +42,42 @@ impl<'tcx> ConstReader<'tcx> {
6042
Some(unsize) => self.is_viewed_unsized_from(ty).then_some(unsize.from),
6143
};
6244

45+
// A pointer without provenance -- handle it separately to avoid ICEs.
46+
if let (None, addr) = place.ptr().into_raw_parts() {
47+
let addr = ConstKind::PtrNoProvenance(addr.bytes().into());
48+
let kind = match (unsize, view_ty) {
49+
(None, _) => match ptr_ty.kind() {
50+
ty::TyKind::RawPtr(..) => addr,
51+
ty::TyKind::Ref(..) => ConstKind::PtrCast {
52+
ptr: Box::new(Const {
53+
ty: Ty::new_ptr(tcx, ty, mutability),
54+
kind: addr,
55+
}),
56+
unsize: None,
57+
},
58+
_ => unreachable!(),
59+
},
60+
// Unsize the thin pointer with the metadata.
61+
(Some(unsize), Some(view_ty)) => ConstKind::PtrCast {
62+
ptr: Box::new(Const {
63+
ty: Ty::new_ptr(tcx, view_ty, mutability),
64+
kind: addr,
65+
}),
66+
unsize: Some(unsize),
67+
},
68+
// We don't have a sized type to unsize from, so we encode the pointer as bytes.
69+
(Some(_), None) => ConstKind::Memory(self.read_raw_bytes(ecx, op)?),
70+
};
71+
return interp_ok(Const { ty: ptr_ty, kind });
72+
}
73+
74+
let (alloc_id, offset, _) = ecx.ptr_get_alloc_id(place.ptr(), 0)?;
75+
// A pointer into the allocation of a `TypeId` can only be expressed as bytes.
76+
if let AllocTarget::TypeId(_) = self.alloc_target(alloc_id) {
77+
let kind = ConstKind::Memory(self.read_raw_bytes(ecx, op)?);
78+
return interp_ok(Const { ty: ptr_ty, kind });
79+
}
80+
6381
let alloc_target = self.alloc_target(alloc_id);
6482
// A pointer outside of its allocation (one-past-the-end is fine) needs a wrapping offset.
6583
let in_bounds = offset <= ecx.get_alloc_info(alloc_id).size;
@@ -77,10 +95,6 @@ impl<'tcx> ConstReader<'tcx> {
7795
ConstKind::Ptr { target, unsize }
7896
} else {
7997
// Point to the place we reached, then cast the pointer to the right type.
80-
let (ty::Ref(.., mutability) | ty::RawPtr(_, mutability)) = *op.layout.ty.kind()
81-
else {
82-
unreachable!()
83-
};
8498
let ptr = |ty, kind| Box::new(Const { ty, kind });
8599
let inner = ptr(
86100
Ty::new_ptr(tcx, end_ty, mutability),
@@ -120,10 +134,7 @@ impl<'tcx> ConstReader<'tcx> {
120134
let target = PtrTarget::Inline(Box::new(self.read_op(ecx, place.into())?));
121135
ConstKind::Ptr { target, unsize }
122136
};
123-
interp_ok(Const {
124-
ty: op.layout.ty,
125-
kind,
126-
})
137+
interp_ok(Const { ty: ptr_ty, kind })
127138
}
128139

129140
/// Whether we view a value of the unsized type `ty` at the sized type it was unsized from: a

‎charon/tests/ui/consts/unsized-globals.initializers.out‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ pub unsafe trait Sync<Self>
2929

3030
vtable core::marker::Sync::{vtable}<Self>: core::marker::Sync::{vtable} = Opaque(VTable was chosen to be opaque)
3131

32+
// Full name: core::slice::raw::from_raw_parts
33+
#[track_caller]
34+
#[diagnostic_item("slice_from_raw_parts")]
35+
pub unsafe fn from_raw_parts<'a, T>(_1: *const T, _2: usize) -> &'a [T]
36+
where
37+
TraitClause0: (T: Sized),
38+
TypeOutlives0: T: 'a,
39+
= <opaque>
40+
3241
struct (_, _)<A, B>
3342
where
3443
TraitClause0: (A: Sized),
@@ -421,6 +430,28 @@ pub fn RAW_DYN() -> RawPtr
421430
// Full name: test_crate::RAW_DYN
422431
pub static RAW_DYN: RawPtr = RAW_DYN()
423432

433+
// Full name: test_crate::EMPTY
434+
pub fn EMPTY() -> &'static [u8]
435+
{
436+
let _0: &'1 [u8]; // return
437+
let _1: &'2 [u8]; // anonymous local
438+
let _2: *const u8; // anonymous local
439+
440+
storage_live(_0)
441+
storage_live(_1)
442+
storage_live(_2)
443+
_2 = cast_with_exposed<*const u8>(const 1usize)
444+
_1 = from_raw_parts<'3, u8>[{built_in impl Sized for u8}](move _2, const 0usize)
445+
↳⚡ unwind_continue
446+
_0 = &(*_1) with_metadata(copy _1.metadata)
447+
storage_dead(_2)
448+
storage_dead(_1)
449+
return
450+
}
451+
452+
// Full name: test_crate::EMPTY
453+
pub static EMPTY: &'static [u8] = EMPTY()
454+
424455
pub const test_crate::inline_const::{promoted_const}: Outer<[u16]>[{built_in impl MetaSized for [u16]}] = Outer { x: 1u32, w: Wrap { a: 2u8, b: [3u16, 4u16] } } with_metadata(2usize)
425456

426457
// Full name: test_crate::inline_const

‎charon/tests/ui/consts/unsized-globals.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub static DYN: &dyn Trait = &8;
3535
pub static FOO: &(dyn Debug + Sync) = &42;
3636
pub static DYN_TAIL: &Wrap<dyn Trait> = &Wrap { a: 9, b: 10u8 };
3737
pub static RAW_DYN: RawPtr = RawPtr(&11u8);
38+
pub static EMPTY: &[u8] = unsafe { core::slice::from_raw_parts(1 as *const u8, 0) };
3839

3940
pub fn inline_const() {
4041
let _: &Outer<[u16]> = const {

‎charon/tests/ui/consts/unsized-globals.values.out‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ pub static DYN_TAIL: &'static Wrap<(dyn Trait + 'static)>[{built_in impl MetaSiz
156156
// Full name: test_crate::RAW_DYN
157157
pub static RAW_DYN: RawPtr = RawPtr { _0: unsize_cast<*const (dyn Trait), &impl_Trait_for_u8::{vtable}>(cast<*const u8>(&raw const test_crate::{promoted_const#2})) }
158158

159+
// Full name: test_crate::EMPTY
160+
pub static EMPTY: &'static [u8] = unsize_cast<&'_ [u8], 0usize>(no-provenance 1)
161+
159162
pub const test_crate::inline_const::{promoted_const}: Outer<[u16]>[{built_in impl MetaSized for [u16]}] = Outer { x: 1u32, w: Wrap { a: 2u8, b: [3u16, 4u16] } } with_metadata(2usize)
160163

161164
// Full name: test_crate::inline_const

0 commit comments

Comments
 (0)