Skip to content

Memory leak checker misses AtomicPtr #1574

Closed
rust-lang/rust
#77611
@cynecx

Description

@cynecx
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 #1618.)

Metadata

Metadata

Assignees

Labels

A-leaksArea: affects the memory leak checkerC-enhancementCategory: a PR with an enhancement or an issue tracking an accepted enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions