Skip to content

Global doesn't respect Send and Sync on the #[global_allocator] type #85487

@repnop

Description

@repnop

I'm working on a hobby operating system and one of the things I'm doing is having allocators in userspace be defaulted to being only single-threaded, and I discovered today with some testing on the playground that the Global allocator type has the Send and Sync autotraits applied, regardless of if the global allocator is Send or Sync (in the example below, it is neither). The expectation is the std::thread::spawn would error as the Box bound to foo should not impl Send, thus preventing the move, as the actual underlying allocator (Foo) isn't Send.

#![feature(thread_local)]

struct Foo(*mut ());

unsafe impl core::alloc::GlobalAlloc for Foo {
    unsafe fn alloc(&self, layout: core::alloc::Layout) -> *mut u8 { /* IRRELEVANT */ std::alloc::System.alloc(layout) }
    unsafe fn dealloc(&self, ptr: *mut u8, layout: core::alloc::Layout) { /* IRRELEVANT */ std::alloc::System.dealloc(ptr, layout) }
}

#[thread_local]
#[global_allocator]
static FOO: Foo = Foo(core::ptr::null_mut());

fn main() {
    let foo = Box::new(0u32);
    let _ = std::thread::spawn(move || println!("{}", foo)).join();
}

playground link

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-allocatorsArea: Custom and system allocatorsC-bugCategory: This is a bug.T-langRelevant to the language teamT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions