@@ -20,10 +20,10 @@ use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
20
20
use tracing:: { debug, instrument, trace} ;
21
21
22
22
use 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,
27
27
} ;
28
28
use crate :: fluent_generated as fluent;
29
29
@@ -230,11 +230,12 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
230
230
size : Size ,
231
231
align : Align ,
232
232
kind : MemoryKind < M :: MemoryKind > ,
233
+ init : AllocInit ,
233
234
) -> InterpResult < ' tcx , Pointer < M :: Provenance > > {
234
235
let alloc = if M :: PANIC_ON_ALLOC_FAIL {
235
- Allocation :: uninit ( size, align)
236
+ Allocation :: new ( size, align, init )
236
237
} else {
237
- Allocation :: try_uninit ( size, align) ?
238
+ Allocation :: try_new ( size, align, init ) ?
238
239
} ;
239
240
self . insert_allocation ( alloc, kind)
240
241
}
@@ -270,13 +271,16 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
270
271
M :: adjust_alloc_root_pointer ( self , Pointer :: from ( id) , Some ( kind) )
271
272
}
272
273
274
+ /// If this grows the allocation, `init_growth` determines
275
+ /// whether the additional space will be initialized.
273
276
pub fn reallocate_ptr (
274
277
& mut self ,
275
278
ptr : Pointer < Option < M :: Provenance > > ,
276
279
old_size_and_align : Option < ( Size , Align ) > ,
277
280
new_size : Size ,
278
281
new_align : Align ,
279
282
kind : MemoryKind < M :: MemoryKind > ,
283
+ init_growth : AllocInit ,
280
284
) -> InterpResult < ' tcx , Pointer < M :: Provenance > > {
281
285
let ( alloc_id, offset, _prov) = self . ptr_get_alloc_id ( ptr, 0 ) ?;
282
286
if offset. bytes ( ) != 0 {
@@ -289,7 +293,10 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
289
293
290
294
// For simplicities' sake, we implement reallocate as "alloc, copy, dealloc".
291
295
// 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) ?;
293
300
let old_size = match old_size_and_align {
294
301
Some ( ( size, _align) ) => size,
295
302
None => self . get_alloc_raw ( alloc_id) ?. size ( ) ,
0 commit comments