Skip to content

Properly deallocate thread locals #1369

Closed
@vakaras

Description

@vakaras

Note: this is currently blocked on rust-lang/rust#70685

Currently Miri has only partial support for deallocation of thread-locals. It runs destructors of the library thread locals (src/shims/tls.rs), but it does not deallocate the thread-local statics #[thread_local]. As a result, Miri misses undefined behaviour in the following program:

#![feature(thread_local)]

use std::thread;

#[thread_local]
static mut A: u8 = 0;

struct Sender(*const u8);

unsafe impl Send for Sender {}

fn main() {
    let handle = thread::spawn(|| {
        let ptr = unsafe { &mut A as *mut u8 };
        assert_eq!(unsafe { *ptr }, 0);
        unsafe { *ptr = 5; }
        Sender(ptr)
    });
    let ptr = handle.join().unwrap().0;
    let x = unsafe { *ptr };  // This should be UB.
    let y = x + 1;
}

Fixing this issue before rust-lang/rust#70685 is very hard because currently we allocate thread local statics in tcx.alloc_map, which allows adding elements, but does not allow removing them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-concurrencyArea: affects our concurrency (multi-thread) supportC-bugCategory: This is a bug.I-misses-UBImpact: makes Miri miss UB, i.e., a false negative (with default settings)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions