diff --git a/async-usercalls/src/alloc/mod.rs b/async-usercalls/src/alloc/mod.rs index ab1085c04..d1a1abff1 100644 --- a/async-usercalls/src/alloc/mod.rs +++ b/async-usercalls/src/alloc/mod.rs @@ -3,13 +3,11 @@ use std::os::fortanix_sgx::usercalls::raw::ByteBuffer; mod allocator; mod bitmap; -mod io_bufs; mod slab; #[cfg(test)] mod tests; use self::allocator::{LocalAllocator, SharedAllocator}; -pub use self::io_bufs::{ReadBuffer, UserBuf, WriteBuffer}; pub use self::slab::{User, UserSafeExt}; /// Allocates a slice of bytes in userspace that is at least as large as `size`. diff --git a/async-usercalls/src/alloc/tests.rs b/async-usercalls/src/alloc/tests.rs index da4e8b3d3..0d9906ab9 100644 --- a/async-usercalls/src/alloc/tests.rs +++ b/async-usercalls/src/alloc/tests.rs @@ -1,6 +1,5 @@ use super::allocator::SharedAllocator; use super::bitmap::*; -use super::io_bufs::{ReadBuffer, UserBuf, WriteBuffer}; use super::slab::{BufSlab, Slab, SlabAllocator, User}; use crossbeam_channel as mpmc; use std::collections::HashSet; @@ -262,62 +261,3 @@ fn alloc_buf_size() { let b = super::alloc_buf(8 * 1024); assert_eq!(b.len(), 8 * 1024); } - -#[test] -fn write_buffer_basic() { - const LENGTH: usize = 1024; - let mut write_buffer = WriteBuffer::new(super::alloc_buf(1024)); - - let buf = vec![0u8; LENGTH]; - assert_eq!(write_buffer.write(&buf), LENGTH); - assert_eq!(write_buffer.write(&buf), 0); - - let chunk = write_buffer.consumable_chunk().unwrap(); - write_buffer.consume(chunk, 200); - assert_eq!(write_buffer.write(&buf), 200); - assert_eq!(write_buffer.write(&buf), 0); -} - -#[test] -#[should_panic] -fn call_consumable_chunk_twice() { - const LENGTH: usize = 1024; - let mut write_buffer = WriteBuffer::new(super::alloc_buf(1024)); - - let buf = vec![0u8; LENGTH]; - assert_eq!(write_buffer.write(&buf), LENGTH); - assert_eq!(write_buffer.write(&buf), 0); - - let chunk1 = write_buffer.consumable_chunk().unwrap(); - let _ = write_buffer.consumable_chunk().unwrap(); - drop(chunk1); -} - -#[test] -#[should_panic] -fn consume_wrong_buf() { - const LENGTH: usize = 1024; - let mut write_buffer = WriteBuffer::new(super::alloc_buf(1024)); - - let buf = vec![0u8; LENGTH]; - assert_eq!(write_buffer.write(&buf), LENGTH); - assert_eq!(write_buffer.write(&buf), 0); - - let unrelated_buf: UserBuf = super::alloc_buf(512).into(); - write_buffer.consume(unrelated_buf, 100); -} - -#[test] -fn read_buffer_basic() { - let mut buf = super::alloc_buf(64); - const DATA: &'static [u8] = b"hello"; - buf[0..DATA.len()].copy_from_enclave(DATA); - - let mut read_buffer = ReadBuffer::new(buf, DATA.len()); - assert_eq!(read_buffer.len(), DATA.len()); - assert_eq!(read_buffer.remaining_bytes(), DATA.len()); - let mut buf = [0u8; 8]; - assert_eq!(read_buffer.read(&mut buf), DATA.len()); - assert_eq!(read_buffer.remaining_bytes(), 0); - assert_eq!(&buf, b"hello\0\0\0"); -} diff --git a/async-usercalls/src/alloc/io_bufs.rs b/async-usercalls/src/io_bufs.rs similarity index 99% rename from async-usercalls/src/alloc/io_bufs.rs rename to async-usercalls/src/io_bufs.rs index 3880e763e..7eb6b6e0c 100644 --- a/async-usercalls/src/alloc/io_bufs.rs +++ b/async-usercalls/src/io_bufs.rs @@ -1,4 +1,4 @@ -use super::slab::User; +use crate::alloc::User; use std::cell::UnsafeCell; use std::cmp; use std::io::IoSlice; diff --git a/async-usercalls/src/lib.rs b/async-usercalls/src/lib.rs index e3a9e5d31..ab2b9e01f 100644 --- a/async-usercalls/src/lib.rs +++ b/async-usercalls/src/lib.rs @@ -15,6 +15,7 @@ mod batch_drop; mod callback; mod duplicated; mod hacks; +mod io_bufs; mod provider_api; mod provider_core; mod queues; @@ -22,9 +23,10 @@ mod raw; #[cfg(test)] mod tests; -pub use self::alloc::{alloc_buf, alloc_byte_buffer, ReadBuffer, User, UserBuf, UserSafeExt, WriteBuffer}; +pub use self::alloc::{alloc_buf, alloc_byte_buffer, User, UserSafeExt}; pub use self::batch_drop::batch_drop; pub use self::callback::CbFn; +pub use self::io_bufs::{ReadBuffer, UserBuf, WriteBuffer}; pub use self::raw::RawApi; use self::callback::*; diff --git a/async-usercalls/src/provider_api.rs b/async-usercalls/src/provider_api.rs index 087a22ee7..0dff9f7f2 100644 --- a/async-usercalls/src/provider_api.rs +++ b/async-usercalls/src/provider_api.rs @@ -1,6 +1,7 @@ -use crate::alloc::{alloc_buf, alloc_byte_buffer, User, UserBuf}; +use crate::alloc::{alloc_buf, alloc_byte_buffer, User}; use crate::batch_drop; use crate::hacks::{new_std_listener, new_std_stream, MakeSend}; +use crate::io_bufs::UserBuf; use crate::raw::RawApi; use crate::{AsyncUsercallProvider, CancelHandle}; use fortanix_sgx_abi::Fd; diff --git a/async-usercalls/src/tests.rs b/async-usercalls/src/tests.rs index a72d6c7d8..406c82443 100644 --- a/async-usercalls/src/tests.rs +++ b/async-usercalls/src/tests.rs @@ -194,6 +194,65 @@ fn safe_alloc_free() { unsafe impl Send for MakeSend> {} +#[test] +fn write_buffer_basic() { + const LENGTH: usize = 1024; + let mut write_buffer = WriteBuffer::new(super::alloc_buf(1024)); + + let buf = vec![0u8; LENGTH]; + assert_eq!(write_buffer.write(&buf), LENGTH); + assert_eq!(write_buffer.write(&buf), 0); + + let chunk = write_buffer.consumable_chunk().unwrap(); + write_buffer.consume(chunk, 200); + assert_eq!(write_buffer.write(&buf), 200); + assert_eq!(write_buffer.write(&buf), 0); +} + +#[test] +#[should_panic] +fn call_consumable_chunk_twice() { + const LENGTH: usize = 1024; + let mut write_buffer = WriteBuffer::new(super::alloc_buf(1024)); + + let buf = vec![0u8; LENGTH]; + assert_eq!(write_buffer.write(&buf), LENGTH); + assert_eq!(write_buffer.write(&buf), 0); + + let chunk1 = write_buffer.consumable_chunk().unwrap(); + let _ = write_buffer.consumable_chunk().unwrap(); + drop(chunk1); +} + +#[test] +#[should_panic] +fn consume_wrong_buf() { + const LENGTH: usize = 1024; + let mut write_buffer = WriteBuffer::new(super::alloc_buf(1024)); + + let buf = vec![0u8; LENGTH]; + assert_eq!(write_buffer.write(&buf), LENGTH); + assert_eq!(write_buffer.write(&buf), 0); + + let unrelated_buf: UserBuf = super::alloc_buf(512).into(); + write_buffer.consume(unrelated_buf, 100); +} + +#[test] +fn read_buffer_basic() { + let mut buf = super::alloc_buf(64); + const DATA: &'static [u8] = b"hello"; + buf[0..DATA.len()].copy_from_enclave(DATA); + + let mut read_buffer = ReadBuffer::new(buf, DATA.len()); + assert_eq!(read_buffer.len(), DATA.len()); + assert_eq!(read_buffer.remaining_bytes(), DATA.len()); + let mut buf = [0u8; 8]; + assert_eq!(read_buffer.read(&mut buf), DATA.len()); + assert_eq!(read_buffer.remaining_bytes(), 0); + assert_eq!(&buf, b"hello\0\0\0"); +} + #[test] #[ignore] fn echo() {