-
Notifications
You must be signed in to change notification settings - Fork 133
Open
Description
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() == 0but this will add an additonal branch for slice allocations and allocations by a non statically known layout - don't use
EMPTY_CHUNKbut always allocate a chunk on creation
mlaveaux
Metadata
Metadata
Assignees
Labels
No labels