@@ -317,7 +317,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
317317 kind = "static_mem"
318318 )
319319 }
320- None => err_ub ! ( PointerUseAfterFree ( alloc_id) ) ,
320+ None => err_ub ! ( PointerUseAfterFree ( alloc_id, CheckInAllocMsg :: MemoryAccessTest ) ) ,
321321 }
322322 . into ( ) ) ;
323323 } ;
@@ -380,7 +380,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
380380 M :: enforce_alignment ( self ) ,
381381 CheckInAllocMsg :: MemoryAccessTest ,
382382 |alloc_id, offset, prov| {
383- let ( size, align) = self . get_live_alloc_size_and_align ( alloc_id) ?;
383+ let ( size, align) = self
384+ . get_live_alloc_size_and_align ( alloc_id, CheckInAllocMsg :: MemoryAccessTest ) ?;
384385 Ok ( ( size, align, ( alloc_id, offset, prov) ) )
385386 } ,
386387 )
@@ -404,7 +405,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
404405 CheckAlignment :: Error ,
405406 msg,
406407 |alloc_id, _, _| {
407- let ( size, align) = self . get_live_alloc_size_and_align ( alloc_id) ?;
408+ let ( size, align) = self . get_live_alloc_size_and_align ( alloc_id, msg ) ?;
408409 Ok ( ( size, align, ( ) ) )
409410 } ,
410411 ) ?;
@@ -414,7 +415,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
414415 /// Low-level helper function to check if a ptr is in-bounds and potentially return a reference
415416 /// to the allocation it points to. Supports both shared and mutable references, as the actual
416417 /// checking is offloaded to a helper closure. `align` defines whether and which alignment check
417- /// is done. Returns `None` for size 0, and otherwise `Some` of what `alloc_size` returned.
418+ /// is done.
419+ ///
420+ /// If this returns `None`, the size is 0; it can however return `Some` even for size 0.
418421 fn check_and_deref_ptr < T > (
419422 & self ,
420423 ptr : Pointer < Option < M :: Provenance > > ,
@@ -515,7 +518,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
515518 }
516519 Some ( GlobalAlloc :: Function ( ..) ) => throw_ub ! ( DerefFunctionPointer ( id) ) ,
517520 Some ( GlobalAlloc :: VTable ( ..) ) => throw_ub ! ( DerefVTablePointer ( id) ) ,
518- None => throw_ub ! ( PointerUseAfterFree ( id) ) ,
521+ None => throw_ub ! ( PointerUseAfterFree ( id, CheckInAllocMsg :: MemoryAccessTest ) ) ,
519522 Some ( GlobalAlloc :: Static ( def_id) ) => {
520523 assert ! ( self . tcx. is_static( def_id) ) ;
521524 assert ! ( !self . tcx. is_thread_local_static( def_id) ) ;
@@ -761,11 +764,15 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
761764 }
762765 }
763766
764- /// Obtain the size and alignment of a live allocation.
765- pub fn get_live_alloc_size_and_align ( & self , id : AllocId ) -> InterpResult < ' tcx , ( Size , Align ) > {
767+ /// Obtain the size and alignment of a *live* allocation.
768+ fn get_live_alloc_size_and_align (
769+ & self ,
770+ id : AllocId ,
771+ msg : CheckInAllocMsg ,
772+ ) -> InterpResult < ' tcx , ( Size , Align ) > {
766773 let ( size, align, kind) = self . get_alloc_info ( id) ;
767774 if matches ! ( kind, AllocKind :: Dead ) {
768- throw_ub ! ( PointerUseAfterFree ( id) )
775+ throw_ub ! ( PointerUseAfterFree ( id, msg ) )
769776 }
770777 Ok ( ( size, align) )
771778 }
0 commit comments