Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Fix logic and double-drop UB in `WriteBuffer` impls (#12870)

Alex Crichtonbc6585e80570
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index fee22950f5..ba58cd0523 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -1205,7 +1205,7 @@ jobs:
strategy:
matrix:
include:
- - crate: "wasmtime"
+ - crate: "wasmtime --features component-model-async-bytes"
- crate: "wasmtime-cli"
- crate: "wasmtime-environ --all-features"
- crate: "pulley-interpreter --all-features"
diff --git a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams/buffers.rs b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams/buffers.rs
index 8a7f2f14c7..894ba19c8c 100644
--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams/buffers.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams/buffers.rs
@@ -256,7 +256,7 @@ unsafe impl WriteBuffer<u8> for SliceBuffer {
// always be sound.
fun(unsafe {
mem::transmute::<&[u8], &[MaybeUninit<u8>]>(
- &self.buffer[self.offset - count..self.limit],
+ &self.buffer[self.offset - count..self.offset],
)
});
}
@@ -330,7 +330,7 @@ unsafe impl<T: Send + Sync + 'static> WriteBuffer<T> for VecBuffer<T> {
// ensure that if `fun` panics that the items are still considered
// transferred.
self.offset += count;
- fun(&self.buffer[self.offset - count..]);
+ fun(&self.buffer[self.offset - count..self.offset]);
}
}
@@ -396,7 +396,7 @@ unsafe impl WriteBuffer<u8> for Cursor<Bytes> {
fn take(&mut self, count: usize, fun: &mut dyn FnMut(&[MaybeUninit<u8>])) {
assert!(count <= self.remaining().len());
- fun(unsafe_byte_slice(self.remaining()));
+ fun(unsafe_byte_slice(&self.remaining()[..count]));
self.skip(count);
}
}
@@ -420,7 +420,7 @@ unsafe impl WriteBuffer<u8> for Cursor<BytesMut> {
fn take(&mut self, count: usize, fun: &mut dyn FnMut(&[MaybeUninit<u8>])) {
assert!(count <= self.remaining().len());
- fun(unsafe_byte_slice(self.remaining()));
+ fun(unsafe_byte_slice(&self.remaining()[..count]));
self.skip(count);
}
}
@@ -453,3 +453,66 @@ fn unsafe_byte_slice(slice: &[u8]) -> &[MaybeUninit<u8>] {
// possibly-initialized slice of items.
unsafe { mem::transmute::<&[u8], &[MaybeUninit<u8>]>(slice) }
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::prelude::*;
+
+ #[test]
+ fn test_vec_buffer_take() {
+ let mut buf = VecBuffer::from(vec!["a".to_string(), "b".to_string(), "c".to_string()]);
+ let mut dst = Vec::new();
+ dst.reserve(1);
+ dst.move_from(&mut buf, 1);
+ assert_eq!(buf.remaining().len(), 2);
+ assert_eq!(dst.len(), 1);
+ None.move_from(&mut buf, 1);
+ assert_eq!(buf.remaining().len(), 1);
+ assert_eq!(dst.len(), 1);
+ }
+
+ #[test]
+ fn test_slice_buffer_take() {
+ let mut buf = SliceBuffer::new(vec![1, 2, 3], 0, 3);
+ let mut dst = Vec::new();
+ dst.reserve(1);
+ dst.move_from(&mut buf, 1);
+ assert_eq!(buf.remaining().len(), 2);
+ assert_eq!(dst.len(), 1);
+ }
+
+ #[test]
+ #[cfg(feature = "component-model-async-bytes")]
+ fn test_cursor_bytes_take() {
+ let mut buf = Cursor::new(Bytes::from(&b"123"[..]));
+ let mut dst = Vec::new();
+ dst.reserve(1);
+ dst.move_from(&mut buf, 1);
+ assert_eq!(buf.remaining().len(), 2);
+ assert_eq!(dst.len(), 1);
+
+ let mut dst = BytesMut::new();
+ dst.reserve(1);
+ dst.move_from(&mut buf, 1);
+ assert_eq!(buf.remaining().len(), 1);
+ assert_eq!(dst.len(), 1);
+ }
+
+ #[test]
+ #[cfg(feature = "component-model-async-bytes")]
+ fn test_cursor_bytes_mut_take() {
+ let mut buf = Cursor::new(BytesMut::from(&b"123"[..]));
+ let mut dst = Vec::new();
+ dst.reserve(1);
+ dst.move_from(&mut buf, 1);
+ assert_eq!(buf.remaining().len(), 2);
+ assert_eq!(dst.len(), 1);
+
+ let mut dst = BytesMut::new();
+ dst.reserve(1);
+ dst.move_from(&mut buf, 1);
+ assert_eq!(buf.remaining().len(), 1);
+ assert_eq!(dst.len(), 1);
+ }
+}