rust-vmm / vm-memory

Virtual machine's guest memory crate
Apache License 2.0
305 stars 98 forks source link

Fix UB in `MockBytesContainer` #228

Closed cuviper closed 1 year ago

cuviper commented 1 year ago

Miri flagged this as a retag error:

error: Undefined Behavior: trying to retag from <wildcard> for
Unique permission at alloc114820[0x0], but no exposed tags have
suitable permission in the borrow stack for this location

... in the slice::from_raw_parts_mut via &self. This also manifested as a test failure in Fedora builds, which run tests in release mode:

---- bytes::tests::test_bytes stdout ----
thread 'bytes::tests::test_bytes' panicked at 'assertion failed: `(left == right)`
  left: `0`,
 right: `18446744073709551615`', src/bytes.rs:509:9

Using RefCell is simple way to get mutable access through &self, and the overhead of that runtime check is irrelevant for test-mocking.