Skip to content

Commit d1eab2f

Browse files
authored
Limit async stream read/write lengths (#1588)
The canonical ABI requires that the maximum size of any one stream operation is `(1 << 28) - 1`, so this commit places that limit on the runtime implementation. This generally isn't applicable but it does get used for `stream` without a payload where otherwise the capacity of vectors is `usize::MAX`, hence large reads/writes.
1 parent a4b3eb1 commit d1eab2f

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

crates/guest-rust/src/rt/async_support/stream_support.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ use {
1717
},
1818
};
1919

20+
/// Maximum size of a read/write operation as specified by the canonical ABI.
21+
const MAX_LENGTH: usize = (1 << 28) - 1;
22+
2023
/// Operations that a stream requires throughout the implementation.
2124
///
2225
/// This is generated by `wit_bindgen::generate!` primarily.
@@ -376,7 +379,11 @@ where
376379
let (ptr, len) = buf.abi_ptr_and_len();
377380
// SAFETY: sure hope this is safe, everything in this module and
378381
// `AbiBuffer` is trying to make this safe.
379-
let code = unsafe { self.writer.ops.start_write(self.writer.handle, ptr, len) };
382+
let code = unsafe {
383+
self.writer
384+
.ops
385+
.start_write(self.writer.handle, ptr, len.min(MAX_LENGTH))
386+
};
380387
rtdebug!(
381388
"stream.write({}, {ptr:?}, {len}) = {code:#x}",
382389
self.writer.handle
@@ -618,7 +625,7 @@ unsafe impl<'a, O: StreamOps> WaitableOp for StreamReadOp<'a, O> {
618625
let code = unsafe {
619626
self.reader
620627
.ops
621-
.start_read(self.reader.handle(), ptr, cap.len())
628+
.start_read(self.reader.handle(), ptr, cap.len().min(MAX_LENGTH))
622629
};
623630
rtdebug!(
624631
"stream.read({}, {ptr:?}, {}) = {code:#x}",

0 commit comments

Comments
 (0)