Skip to content

Data unsoundness #118

Open
Open

Description

@Roshyoshi

in_memory::Data<Row, N> is Sync for every Row, and save_raw_row takes &self and makes a &mut to the whole page buffer through an UnsafeCell before it copies the row in. Two threads that share a page and both call it hold &mut to the same buffer at the same time. Safe code can then cause a data race, which is undefined behavior.

Found in worktable 1.13.3 from crates.io. Still present in 1.13.7 and on master at 9969620.

Where

unsafe impl<Row, const DATA_LENGTH: usize> Sync for Data<Row, DATA_LENGTH> {}

let inner_data = unsafe { &mut *self.inner_data.get() };
inner_data[offset as usize..][..length as usize].copy_from_slice(data);

Reproducer

[dependencies]
worktable = "=1.13.7"
#![forbid(unsafe_code)]
use worktable::in_memory::Data;

fn main() {
    let page = Data::<(), 16>::new(Default::default());
    std::thread::scope(|s| {
        s.spawn(|| page.save_raw_row(&[1]).unwrap());
        page.save_raw_row(&[2]).unwrap();
    });
}
$ cargo +nightly miri run
error: Undefined Behavior: Data race detected between (1) non-atomic write on thread `main` and (2) retag write of type `worktable::in_memory::data::AlignedBytes<16>` on thread `unnamed-1` at alloc16935+0x4840
   --> worktable/src/in_memory/data.rs:987:35
    |
987 |         let inner_data = unsafe { &mut *self.inner_data.get() };
    |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^ (2) just happened here
    |
help: and (1) occurred earlier here
   --> src/main.rs:8:9
    |
  8 |         page.save_raw_row(&[2]).unwrap();
    |         ^^^^^^^^^^^^^^^^^^^^^^^

Notes

  • The two calls write different bytes: each one first reserves its own range in free_offset. What Miri reports is the two &mut references to the whole buffer.
  • The program uses 1.13.7 because a fresh build of =1.13.3 no longer compiles (E0277 on nagoya::io::HostFile::new).
  • Tested with rustc 1.101.0-nightly (1c3a65069 2026-11-02) and its Miri on aarch64-apple-darwin.

Found by @AlexLB99 during a run of an automated dynamic analysis tool.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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