Skip to content

Commit 9506011

Browse files
committed
Fix allocator shim handling in miri
1 parent 8ea28a4 commit 9506011

File tree

2 files changed

+32
-31
lines changed

2 files changed

+32
-31
lines changed

src/tools/miri/src/machine.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,10 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
635635

636636
/// Sets up the "extern statics" for this machine.
637637
fn init_extern_statics(this: &mut MiriInterpCx<'mir, 'tcx>) -> InterpResult<'tcx> {
638+
// "__rust_no_alloc_shim_is_unstable"
639+
let val = ImmTy::from_int(0, this.machine.layouts.u8);
640+
Self::alloc_extern_static(this, "__rust_no_alloc_shim_is_unstable", val)?;
641+
638642
match this.tcx.sess.target.os.as_ref() {
639643
"linux" => {
640644
// "environ"

src/tools/miri/src/shims/foreign_items.rs

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
347347
/// Emulates calling the internal __rust_* allocator functions
348348
fn emulate_allocator(
349349
&mut self,
350-
symbol: Symbol,
351350
default: impl FnOnce(&mut MiriInterpCx<'mir, 'tcx>) -> InterpResult<'tcx>,
352351
) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
353352
let this = self.eval_context_mut();
@@ -359,11 +358,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
359358

360359
match allocator_kind {
361360
AllocatorKind::Global => {
362-
let (body, instance) = this
363-
.lookup_exported_symbol(symbol)?
364-
.expect("symbol should be present if there is a global allocator");
365-
366-
Ok(EmulateByNameResult::MirBody(body, instance))
361+
// `__rust_*` is defined by `#[global_allocator]` if `#[global_allocator]` is used
362+
return Ok(EmulateByNameResult::NotSupported);
367363
}
368364
AllocatorKind::Default => {
369365
default(this)?;
@@ -558,11 +554,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
558554

559555
// Rust allocation
560556
"__rust_alloc" | "miri_alloc" => {
561-
let [size, align] = this.check_shim(abi, Abi::Rust, link_name, args)?;
562-
let size = this.read_target_usize(size)?;
563-
let align = this.read_target_usize(align)?;
564-
565557
let default = |this: &mut MiriInterpCx<'mir, 'tcx>| {
558+
let [size, align] = this.check_shim(abi, Abi::Rust, link_name, args)?;
559+
let size = this.read_target_usize(size)?;
560+
let align = this.read_target_usize(align)?;
561+
566562
Self::check_alloc_request(size, align)?;
567563

568564
let memory_kind = match link_name.as_str() {
@@ -581,8 +577,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
581577
};
582578

583579
match link_name.as_str() {
584-
"__rust_alloc" =>
585-
return this.emulate_allocator(Symbol::intern("__rg_alloc"), default),
580+
"__rust_alloc" => return this.emulate_allocator(default),
586581
"miri_alloc" => {
587582
default(this)?;
588583
return Ok(EmulateByNameResult::NeedsJumping);
@@ -591,11 +586,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
591586
}
592587
}
593588
"__rust_alloc_zeroed" => {
594-
let [size, align] = this.check_shim(abi, Abi::Rust, link_name, args)?;
595-
let size = this.read_target_usize(size)?;
596-
let align = this.read_target_usize(align)?;
589+
return this.emulate_allocator(|this| {
590+
let [size, align] = this.check_shim(abi, Abi::Rust, link_name, args)?;
591+
let size = this.read_target_usize(size)?;
592+
let align = this.read_target_usize(align)?;
597593

598-
return this.emulate_allocator(Symbol::intern("__rg_alloc_zeroed"), |this| {
599594
Self::check_alloc_request(size, align)?;
600595

601596
let ptr = this.allocate_ptr(
@@ -614,12 +609,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
614609
});
615610
}
616611
"__rust_dealloc" | "miri_dealloc" => {
617-
let [ptr, old_size, align] = this.check_shim(abi, Abi::Rust, link_name, args)?;
618-
let ptr = this.read_pointer(ptr)?;
619-
let old_size = this.read_target_usize(old_size)?;
620-
let align = this.read_target_usize(align)?;
621-
622612
let default = |this: &mut MiriInterpCx<'mir, 'tcx>| {
613+
let [ptr, old_size, align] =
614+
this.check_shim(abi, Abi::Rust, link_name, args)?;
615+
let ptr = this.read_pointer(ptr)?;
616+
let old_size = this.read_target_usize(old_size)?;
617+
let align = this.read_target_usize(align)?;
618+
623619
let memory_kind = match link_name.as_str() {
624620
"__rust_dealloc" => MiriMemoryKind::Rust,
625621
"miri_dealloc" => MiriMemoryKind::Miri,
@@ -635,8 +631,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
635631
};
636632

637633
match link_name.as_str() {
638-
"__rust_dealloc" =>
639-
return this.emulate_allocator(Symbol::intern("__rg_dealloc"), default),
634+
"__rust_dealloc" => {
635+
return this.emulate_allocator(default);
636+
}
640637
"miri_dealloc" => {
641638
default(this)?;
642639
return Ok(EmulateByNameResult::NeedsJumping);
@@ -645,15 +642,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
645642
}
646643
}
647644
"__rust_realloc" => {
648-
let [ptr, old_size, align, new_size] =
649-
this.check_shim(abi, Abi::Rust, link_name, args)?;
650-
let ptr = this.read_pointer(ptr)?;
651-
let old_size = this.read_target_usize(old_size)?;
652-
let align = this.read_target_usize(align)?;
653-
let new_size = this.read_target_usize(new_size)?;
654-
// No need to check old_size; we anyway check that they match the allocation.
645+
return this.emulate_allocator(|this| {
646+
let [ptr, old_size, align, new_size] =
647+
this.check_shim(abi, Abi::Rust, link_name, args)?;
648+
let ptr = this.read_pointer(ptr)?;
649+
let old_size = this.read_target_usize(old_size)?;
650+
let align = this.read_target_usize(align)?;
651+
let new_size = this.read_target_usize(new_size)?;
652+
// No need to check old_size; we anyway check that they match the allocation.
655653

656-
return this.emulate_allocator(Symbol::intern("__rg_realloc"), |this| {
657654
Self::check_alloc_request(new_size, align)?;
658655

659656
let align = Align::from_bytes(align).unwrap();

0 commit comments

Comments
 (0)