WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Data race when allocating a zero sized layout #289

@bluurryy

Description

@bluurryy

I have noticed that miri reports a datarace when allocating a zero sized layout from multiple threads on a new Bump:

fn create_bump_and_alloc() {
    let bump = bumpalo::Bump::new();
    bump.alloc(());
}

pub fn main() {
    std::thread::spawn(create_bump_and_alloc);
    std::thread::spawn(create_bump_and_alloc);
}

reports:

error: Undefined Behavior: Data race detected between (1) non-atomic write on thread `unnamed-1` and (2) non-atomic read on thread `unnamed-2` at alloc3054+0x20
   --> .../nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/cell.rs:546:18
    |
546 |         unsafe { *self.value.get() }
    |                  ^^^^^^^^^^^^^^^^^ (2) just happened here
    |
help: and (1) occurred earlier here
   --> src/main.rs:3:5
    |
  3 |     bump.alloc(());
    |     ^^^^^^^^^^^^^^

This happens because the EMPTY_CHUNK footer is shared by all newly created Bumps. When doing a zero sized allocation the empty chunk has enough capacity for the allocation so its pointer gets bumped by 0 and then written back. The empty chunk is not actually modified, but its ptr field is written to with the same value it already has.

I don't know if this could cause problems in practice.

A way to fix this could be:

  • check for layout.size() == 0 but this will add an additonal branch for slice allocations and allocations by a non statically known layout
  • don't use EMPTY_CHUNK but always allocate a chunk on creation

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions