22use crate :: * ;
33use interpret:: GlobalAlloc ;
44use rustc_abi:: { FieldIdx , Size } ;
5+ use rustc_attr_ir:: LangItem ;
56use rustc_const_eval:: const_eval:: CompileTimeInterpCx ;
67use rustc_const_eval:: interpret:: {
78 FnVal , ImmTy , Immediate , InterpResult , MPlaceTy , OpTy , Projectable , interp_ok,
@@ -68,9 +69,11 @@ impl<'tcx> ConstReader<'tcx> {
6869 }
6970 }
7071 interpret:: Scalar :: Ptr ( ptr, size) => {
71- let target = self . alloc_target ( ptr. provenance . alloc_id ( ) ) ;
72+ let ( prov, ptr_offset) = ptr. into_raw_parts ( ) ;
73+ let target = self . alloc_target ( prov. alloc_id ( ) ) ;
7274 for i in 0 ..size. get ( ) {
73- bytes[ offset. bytes_usize ( ) + i as usize ] = Byte :: Ptr ( target, i) ;
75+ bytes[ offset. bytes_usize ( ) + i as usize ] =
76+ self . ptr_byte ( target, ptr_offset, i) ;
7477 }
7578 }
7679 } ;
@@ -114,12 +117,25 @@ impl<'tcx> ConstReader<'tcx> {
114117 . collect ( ) ;
115118
116119 // A pointer may straddle the boundaries of `range`.
120+ let ptr_size = self . tcx . data_layout . pointer_size ( ) ;
117121 for ( prov_range, prov) in alloc. provenance ( ) . get_range ( range, ecx) {
118122 let target = self . alloc_target ( prov. alloc_id ( ) ) ;
123+ // This is either a whole pointer, or a single byte of one whose index and pointer
124+ // bytes are stored with its provenance.
125+ let ( first_idx, ptr_bytes) = if prov_range. size == ptr_size {
126+ ( 0 , alloc. get_bytes_unchecked ( prov_range) )
127+ } else {
128+ let frag = alloc. provenance ( ) . get_byte ( prov_range. start , ecx) . unwrap ( ) ;
129+ ( frag. idx , & frag. bytes [ ..ptr_size. bytes_usize ( ) ] )
130+ } ;
131+ let ptr_offset =
132+ interpret:: read_target_uint ( self . tcx . data_layout . endian , ptr_bytes) . unwrap ( ) ;
133+ let ptr_offset = Size :: from_bytes ( ptr_offset) ;
119134 for i in 0 ..prov_range. size . bytes ( ) {
120135 let pos = prov_range. start + Size :: from_bytes ( i) ;
121136 if range. start <= pos && pos < range. end ( ) {
122- bytes[ ( pos - range. start ) . bytes_usize ( ) ] = Byte :: Ptr ( target, i as u8 ) ;
137+ bytes[ ( pos - range. start ) . bytes_usize ( ) ] =
138+ self . ptr_byte ( target, ptr_offset, first_idx + i as u8 ) ;
123139 }
124140 }
125141 }
@@ -146,6 +162,54 @@ impl<'tcx> ConstReader<'tcx> {
146162 } )
147163 }
148164
165+ /// The type whose `TypeId` `op` is.
166+ fn read_type_id (
167+ & self ,
168+ ecx : & CompileTimeInterpCx < ' tcx > ,
169+ op : & OpTy < ' tcx > ,
170+ ) -> InterpResult < ' tcx , Option < Ty < ' tcx > > > {
171+ let bytes = self . read_raw_bytes ( ecx, op) ?;
172+ if let Some ( & Byte :: Ptr ( AllocTarget :: TypeId ( ty) , ..) ) = bytes. first ( )
173+ && bytes. iter ( ) . enumerate ( ) . all ( |( k, & byte) | {
174+ matches ! ( byte, Byte :: Ptr ( AllocTarget :: TypeId ( byte_ty) , offset, i)
175+ if byte_ty == ty && offset == Size :: ZERO && i as usize == k)
176+ } )
177+ {
178+ return interp_ok ( Some ( ty) ) ;
179+ }
180+ interp_ok ( None )
181+ }
182+
183+ /// The byte at index `i` of a pointer at `offset` into `target`.
184+ ///
185+ /// Use this, rather than building a [`Byte::Ptr`] manually, as this does some tricks to
186+ /// avoid leaking the hash of `TypeId` targets (which are encoded in the pointer's offset).
187+ fn ptr_byte ( & self , target : AllocTarget < ' tcx > , offset : Size , i : u8 ) -> Byte < ' tcx > {
188+ if let AllocTarget :: TypeId ( ty) = target {
189+ let ptr_size = self . tcx . data_layout . pointer_size ( ) ;
190+ let hash = self . type_id_hash ( ty) ;
191+ // if nothing matches, we assume chunk 0 and leak the (relative) offset :(
192+ let chunk = hash. iter ( ) . position ( |& h| h == offset. bytes ( ) ) . unwrap_or ( 0 ) ;
193+ let offset = ptr_size. truncate ( offset. bytes ( ) . wrapping_sub ( hash[ chunk] ) . into ( ) ) ;
194+ let i = chunk as u8 * ptr_size. bytes ( ) as u8 + i;
195+ return Byte :: Ptr ( target, Size :: from_bytes ( offset) , i) ;
196+ }
197+ Byte :: Ptr ( target, offset, i)
198+ }
199+
200+ /// The values of the pointers that make up the `TypeId` of `ty`.
201+ fn type_id_hash ( & self , ty : Ty < ' tcx > ) -> Vec < u64 > {
202+ let dl = & self . tcx . data_layout ;
203+ let hash = self . tcx . type_id_hash ( ty) . as_u128 ( ) ;
204+ let hash = match dl. endian {
205+ rustc_abi:: Endian :: Little => hash. to_le_bytes ( ) ,
206+ rustc_abi:: Endian :: Big => hash. to_be_bytes ( ) ,
207+ } ;
208+ hash. chunks ( dl. pointer_size ( ) . bytes_usize ( ) )
209+ . map ( |chunk| interpret:: read_target_uint ( dl. endian , chunk) . unwrap ( ) as u64 )
210+ . collect ( )
211+ }
212+
149213 /// Read an evaluated operand back as a structured constant.
150214 pub ( crate ) fn read_op (
151215 & self ,
@@ -171,6 +235,12 @@ impl<'tcx> ConstReader<'tcx> {
171235 ty:: Adt ( adt_def, ..) if adt_def. is_union ( ) => {
172236 ConstKind :: Memory ( self . read_raw_bytes ( ecx, & op) ?)
173237 }
238+ ty:: Adt ( adt_def, ..)
239+ if self . tcx . is_lang_item ( adt_def. did ( ) , LangItem :: TypeId )
240+ && let Some ( ty) = self . read_type_id ( ecx, & op) ? =>
241+ {
242+ ConstKind :: TypeId ( ty)
243+ }
174244 ty:: Adt ( adt_def, ..) => {
175245 let variant = ecx. read_discriminant ( & op) ?;
176246 let op = if adt_def. is_enum ( ) {
0 commit comments