Skip to content

Crash during thread teardown using TLS in global allocator on macOS #115149

Open
@jrose-signal

Description

@jrose-signal

I tried this code using accounting_allocator:

use accounting_allocator::AccountingAlloc; // accounting_allocator = "0.1" or "0.2"

#[global_allocator]
static GLOBAL_ALLOCATOR: AccountingAlloc = AccountingAlloc::new();

fn main() {
    let handle = std::thread::spawn(|| println!("Hello, world!"));
    handle.join().expect("ok")
}

On macOS Ventura 13.5 (x86_64-apple-darwin or aarch64-apple-darwin), this code executes successfully when compiled with Rust 1.69.0, but crashes when compiled with Rust 1.70 or later, including the latest nightly. The code executes successfully on Ubuntu.

Using thread-local storage in a global allocator is absolutely a heck of a thing to do, but the code is carefully using LocalKey::try_with to avoid any issues during teardown, and it seemed to be working previously/elsewhere.

Thread 1 Crashed:
0   ???                           	               0x0 ???
1   accounting-allocator-test     	       0x104a96c79 std::sys::unix::thread_local_dtor::register_dtor::run_dtors::hfb57215a22a25a2b + 89
2   dyld                          	    0x7ff801a52fda invocation function for block in dyld4::RuntimeState::_finalizeListTLV(void*) + 53
3   dyld                          	    0x7ff801a52f6b dyld4::RuntimeState::_finalizeListTLV(void*) + 79
4   libsystem_pthread.dylib       	    0x7ff801d9e1e2 _pthread_tsd_cleanup + 465
5   libsystem_pthread.dylib       	    0x7ff801da0873 _pthread_exit + 70
6   libsystem_pthread.dylib       	    0x7ff801da01de _pthread_start + 136
7   libsystem_pthread.dylib       	    0x7ff801d9bbd3 thread_start + 15

Meta

rustc --version --verbose:

rustc 1.71.0 (8ede3aae2 2023-07-12)
binary: rustc
commit-hash: 8ede3aae28fe6e4d52b38157d7bfe0d3bceef225
commit-date: 2023-07-12
host: x86_64-apple-darwin
release: 1.71.0
LLVM version: 16.0.5

(working version)

rustc 1.69.0 (84c898d65 2023-04-16)
binary: rustc
commit-hash: 84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc
commit-date: 2023-04-16
host: x86_64-apple-darwin
release: 1.69.0
LLVM version: 15.0.7

My colleague tested with 1.69.0 (works), 1.70.0 (fails), 1.71.0 (fails), 1.71.1 (fails), and the latest nightly (fails):

rustc 1.74.0-nightly (c469197b1 2023-08-22)
binary: rustc
commit-hash: c469197b19d53a6c45378568f73c00986b20a5a5
commit-date: 2023-08-22
host: x86_64-apple-darwin
release: 1.74.0-nightly
LLVM version: 17.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-allocatorsArea: Custom and system allocatorsA-threadArea: `std::thread`A-thread-localsArea: Thread local storage (TLS)C-bugCategory: This is a bug.O-macosOperating system: macOST-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions