@@ -20,10 +20,10 @@ use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
2020use tracing:: { debug, instrument, trace} ;
2121
2222use super :: {
23- AllocBytes , AllocId , AllocMap , AllocRange , Allocation , CheckAlignMsg , CheckInAllocMsg ,
24- CtfeProvenance , GlobalAlloc , InterpCx , InterpResult , Machine , MayLeak , Misalignment , Pointer ,
25- PointerArithmetic , Provenance , Scalar , alloc_range, err_ub, err_ub_custom , interp_ok , throw_ub ,
26- throw_ub_custom, throw_unsup, throw_unsup_format,
23+ AllocBytes , AllocId , AllocInit , AllocMap , AllocRange , Allocation , CheckAlignMsg ,
24+ CheckInAllocMsg , CtfeProvenance , GlobalAlloc , InterpCx , InterpResult , Machine , MayLeak ,
25+ Misalignment , Pointer , PointerArithmetic , Provenance , Scalar , alloc_range, err_ub,
26+ err_ub_custom , interp_ok , throw_ub , throw_ub_custom, throw_unsup, throw_unsup_format,
2727} ;
2828use crate :: fluent_generated as fluent;
2929
@@ -230,11 +230,12 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
230230 size : Size ,
231231 align : Align ,
232232 kind : MemoryKind < M :: MemoryKind > ,
233+ init : AllocInit ,
233234 ) -> InterpResult < ' tcx , Pointer < M :: Provenance > > {
234235 let alloc = if M :: PANIC_ON_ALLOC_FAIL {
235- Allocation :: uninit ( size, align)
236+ Allocation :: new ( size, align, init )
236237 } else {
237- Allocation :: try_uninit ( size, align) ?
238+ Allocation :: try_new ( size, align, init ) ?
238239 } ;
239240 self . insert_allocation ( alloc, kind)
240241 }
@@ -270,13 +271,16 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
270271 M :: adjust_alloc_root_pointer ( self , Pointer :: from ( id) , Some ( kind) )
271272 }
272273
274+ /// If this grows the allocation, `init_growth` determines
275+ /// whether the additional space will be initialized.
273276 pub fn reallocate_ptr (
274277 & mut self ,
275278 ptr : Pointer < Option < M :: Provenance > > ,
276279 old_size_and_align : Option < ( Size , Align ) > ,
277280 new_size : Size ,
278281 new_align : Align ,
279282 kind : MemoryKind < M :: MemoryKind > ,
283+ init_growth : AllocInit ,
280284 ) -> InterpResult < ' tcx , Pointer < M :: Provenance > > {
281285 let ( alloc_id, offset, _prov) = self . ptr_get_alloc_id ( ptr, 0 ) ?;
282286 if offset. bytes ( ) != 0 {
@@ -289,7 +293,10 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
289293
290294 // For simplicities' sake, we implement reallocate as "alloc, copy, dealloc".
291295 // This happens so rarely, the perf advantage is outweighed by the maintenance cost.
292- let new_ptr = self . allocate_ptr ( new_size, new_align, kind) ?;
296+ // If requested, we zero-init the entire allocation, to ensure that a growing
297+ // allocation has its new bytes properly set. For the part that is copied,
298+ // `mem_copy` below will de-initialize things as necessary.
299+ let new_ptr = self . allocate_ptr ( new_size, new_align, kind, init_growth) ?;
293300 let old_size = match old_size_and_align {
294301 Some ( ( size, _align) ) => size,
295302 None => self . get_alloc_raw ( alloc_id) ?. size ( ) ,
0 commit comments