miri: Memory leak checker misses AtomicPtr

use std::{
    cell::UnsafeCell,
    ptr,
    sync::atomic::{AtomicPtr, Ordering},
};

fn leak() {
    static LEAK: AtomicPtr<usize> = AtomicPtr::new(ptr::null_mut());
    LEAK.store(Box::into_raw(Box::new(0usize)), Ordering::SeqCst);
}

fn no_leak() {
    struct Local(UnsafeCell<*mut usize>);

    unsafe impl Send for Local {}
    unsafe impl Sync for Local {}

    static LEAK: Local = Local(UnsafeCell::new(ptr::null_mut()));
    *unsafe { &mut *LEAK.0.get() } = Box::into_raw(Box::new(0usize));
}

fn main() {
    no_leak();
    // leak();
}

Miri considers memory leaks reachable through a static global as non-leaking. I’d expect the Atomic* case be non-leaking as well, however miri reports it as leaked memory.

The following memory was leaked: alloc1834 (Rust heap, size: 8, align: 8) {
    00 00 00 00 00 00 00 00                         │ ........
}

error: the evaluated program leaked memory

error: aborting due to previous error; 1 warning emitted

(This is a special case of https://github.com/rust-lang/miri/issues/1618.)

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18 (14 by maintainers)

Commits related to this issue

Most upvoted comments

Please open a new issue with a pointer to the relevant code. 😃

Yes. We need to fix this in libstd/rustc, miri is doing the right thing ā„¢ļø and implementing workarounds just on the miri side is not feasible afaict