@@ -358,7 +358,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
358358
359359 match allocator_kind {
360360 AllocatorKind :: Global => {
361- // `__rust_*` is defined by `#[global_allocator]` if `#[global_allocator]` is used
361+ // When `#[global_allocator]` is used, `__rust_*` is defined by the macro expansion
362+ // of this attribute rather than generated by the allocator shim. As such we have
363+ // to call the definition produced by `#[global_allocator]` instead of the shim like
364+ // in the case of `#[global_allocator]` not existing. Somewhat unintuitively doing
365+ // so is done by returning `NotSupported`.
362366 return Ok ( EmulateByNameResult :: NotSupported ) ;
363367 }
364368 AllocatorKind :: Default => {
@@ -555,6 +559,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
555559 // Rust allocation
556560 "__rust_alloc" | "miri_alloc" => {
557561 let default = |this : & mut MiriInterpCx < ' mir , ' tcx > | {
562+ // Only call `check_shim` when `#[global_allocator]` isn't used. The macro
563+ // expansion of `#[global_allocator]` defines this symbol and `check_shim`
564+ // checks that there exists no definition of a shim.
558565 let [ size, align] = this. check_shim ( abi, Abi :: Rust , link_name, args) ?;
559566 let size = this. read_target_usize ( size) ?;
560567 let align = this. read_target_usize ( align) ?;
@@ -587,6 +594,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
587594 }
588595 "__rust_alloc_zeroed" => {
589596 return this. emulate_allocator ( |this| {
597+ // See the comment for `__rust_alloc` why `check_shim` is only called in the
598+ // default case.
590599 let [ size, align] = this. check_shim ( abi, Abi :: Rust , link_name, args) ?;
591600 let size = this. read_target_usize ( size) ?;
592601 let align = this. read_target_usize ( align) ?;
@@ -610,6 +619,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
610619 }
611620 "__rust_dealloc" | "miri_dealloc" => {
612621 let default = |this : & mut MiriInterpCx < ' mir , ' tcx > | {
622+ // See the comment for `__rust_alloc` why `check_shim` is only called in the
623+ // default case.
613624 let [ ptr, old_size, align] =
614625 this. check_shim ( abi, Abi :: Rust , link_name, args) ?;
615626 let ptr = this. read_pointer ( ptr) ?;
@@ -643,6 +654,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
643654 }
644655 "__rust_realloc" => {
645656 return this. emulate_allocator ( |this| {
657+ // See the comment for `__rust_alloc` why `check_shim` is only called in the
658+ // default case.
646659 let [ ptr, old_size, align, new_size] =
647660 this. check_shim ( abi, Abi :: Rust , link_name, args) ?;
648661 let ptr = this. read_pointer ( ptr) ?;
0 commit comments