@@ -33,7 +33,7 @@ use crate::ty;
33
33
pub struct Allocation < Prov = AllocId , Extra = ( ) > {
34
34
/// The actual bytes of the allocation.
35
35
/// Note that the bytes of a pointer represent the offset of the pointer.
36
- bytes : Box < [ u8 ] > ,
36
+ pub bytes : Box < [ u8 ] > ,
37
37
/// Maps from byte addresses to extra data for each pointer.
38
38
/// Only the first byte of a pointer is inserted into the map; i.e.,
39
39
/// every entry in this map applies to `pointer_size` consecutive bytes starting
@@ -207,8 +207,23 @@ impl<Prov> Allocation<Prov> {
207
207
align : Align ,
208
208
mutability : Mutability ,
209
209
) -> Self {
210
- let bytes = Box :: < [ u8 ] > :: from ( slice. into ( ) ) ;
211
- let size = Size :: from_bytes ( bytes. len ( ) ) ;
210
+ let slice: Cow < ' a , [ u8 ] > = slice. into ( ) ;
211
+ let size = Size :: from_bytes ( slice. len ( ) ) ;
212
+ // TODO come up with a cleaner way to align the box based on the alignment parameter
213
+ let count128 = ( slice. len ( ) / 16 ) + 1 ;
214
+ let mut vec128: Vec < u128 > = Vec :: with_capacity ( count128) ;
215
+ // TODO avoid excess initialization
216
+ vec128. resize ( count128, 0 ) ;
217
+ let ( buf, _len, _capacity) = vec128. into_raw_parts ( ) ;
218
+ assert ! ( ( buf as u64 ) % 16 == 0 ) ;
219
+ let vec8: Vec < u8 > = unsafe { Vec :: from_raw_parts ( buf as * mut u8 , size. bytes_usize ( ) , size. bytes_usize ( ) ) } ;
220
+ assert ! ( vec8. as_ptr( ) as u64 % 16 == 0 ) ;
221
+ //let mut vec8: Vec<u8> = unsafe {Vec::from_raw_parts(buf as *mut u8, len * 16, capacity * 16)};
222
+ //vec8.truncate(size.bytes_usize());
223
+ let mut bytes = vec8. into_boxed_slice ( ) ;
224
+ assert ! ( bytes. as_ptr( ) as u64 % 16 == 0 ) ;
225
+ bytes. copy_from_slice ( & slice) ;
226
+
212
227
Self {
213
228
bytes,
214
229
relocations : Relocations :: new ( ) ,
@@ -228,7 +243,17 @@ impl<Prov> Allocation<Prov> {
228
243
///
229
244
/// If `panic_on_fail` is true, this will never return `Err`.
230
245
pub fn uninit < ' tcx > ( size : Size , align : Align , panic_on_fail : bool ) -> InterpResult < ' tcx , Self > {
231
- let bytes = Box :: < [ u8 ] > :: try_new_zeroed_slice ( size. bytes_usize ( ) ) . map_err ( |_| {
246
+ let count128 = ( size. bytes_usize ( ) / 16 ) + 1 ;
247
+ // TODO this one is supposed to handle allocation failure, so do so.
248
+ let mut vec128: Vec < u128 > = Vec :: with_capacity ( count128) ;
249
+ vec128. resize ( count128, 0 ) ;
250
+ let ( buf, _len, _capacity) = vec128. into_raw_parts ( ) ;
251
+ assert ! ( ( buf as u64 ) % 16 == 0 ) ;
252
+ let vec8: Vec < u8 > = unsafe { Vec :: from_raw_parts ( buf as * mut u8 , size. bytes_usize ( ) , size. bytes_usize ( ) ) } ;
253
+ assert ! ( vec8. as_ptr( ) as u64 % 16 == 0 ) ;
254
+ let bytes = vec8. into_boxed_slice ( ) ;
255
+ assert ! ( bytes. as_ptr( ) as u64 % 16 == 0 ) ;
256
+ Ok ( ( ) ) . map_err ( |_: ( ) | {
232
257
// This results in an error that can happen non-deterministically, since the memory
233
258
// available to the compiler can change between runs. Normally queries are always
234
259
// deterministic. However, we can be non-deterministic here because all uses of const
@@ -242,8 +267,6 @@ impl<Prov> Allocation<Prov> {
242
267
} ) ;
243
268
InterpError :: ResourceExhaustion ( ResourceExhaustionInfo :: MemoryExhausted )
244
269
} ) ?;
245
- // SAFETY: the box was zero-allocated, which is a valid initial value for Box<[u8]>
246
- let bytes = unsafe { bytes. assume_init ( ) } ;
247
270
Ok ( Allocation {
248
271
bytes,
249
272
relocations : Relocations :: new ( ) ,
@@ -265,7 +288,14 @@ impl Allocation {
265
288
mut adjust_ptr : impl FnMut ( Pointer < AllocId > ) -> Result < Pointer < Prov > , Err > ,
266
289
) -> Result < Allocation < Prov , Extra > , Err > {
267
290
// Compute new pointer provenance, which also adjusts the bytes.
268
- let mut bytes = self . bytes ;
291
+ // Realign the pointer
292
+ let count128 = ( self . bytes . len ( ) / 16 ) + 1 ;
293
+ let mut vec128: Vec < u128 > = Vec :: with_capacity ( count128) ;
294
+ vec128. resize ( count128, 0 ) ;
295
+ let ( buf, _len, _capacity) = vec128. into_raw_parts ( ) ;
296
+ let mut vec8: Vec < u8 > = unsafe { Vec :: from_raw_parts ( buf as * mut u8 , self . bytes . len ( ) , self . bytes . len ( ) ) } ;
297
+ vec8[ ..self . bytes . len ( ) ] . copy_from_slice ( & self . bytes ) ;
298
+ let mut bytes = vec8. into_boxed_slice ( ) ;
269
299
let mut new_relocations = Vec :: with_capacity ( self . relocations . 0 . len ( ) ) ;
270
300
let ptr_size = cx. data_layout ( ) . pointer_size . bytes_usize ( ) ;
271
301
let endian = cx. data_layout ( ) . endian ;
@@ -278,6 +308,7 @@ impl Allocation {
278
308
write_target_uint ( endian, ptr_bytes, ptr_offset. bytes ( ) . into ( ) ) . unwrap ( ) ;
279
309
new_relocations. push ( ( offset, ptr_prov) ) ;
280
310
}
311
+ assert ! ( bytes. as_ptr( ) as u64 % 16 == 0 ) ;
281
312
// Create allocation.
282
313
Ok ( Allocation {
283
314
bytes,
0 commit comments