Skip to content

Commit

Permalink
Move io_bufs out of alloc module
Browse files Browse the repository at this point in the history
  • Loading branch information
mzohreva committed Nov 4, 2020
1 parent 3be5551 commit 1c64d6d
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 65 deletions.
2 changes: 0 additions & 2 deletions async-usercalls/src/alloc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
60 changes: 0 additions & 60 deletions async-usercalls/src/alloc/tests.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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");
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::slab::User;
use crate::alloc::User;
use std::cell::UnsafeCell;
use std::cmp;
use std::io::IoSlice;
Expand Down
4 changes: 3 additions & 1 deletion async-usercalls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ mod batch_drop;
mod callback;
mod duplicated;
mod hacks;
mod io_bufs;
mod provider_api;
mod provider_core;
mod queues;
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::*;
Expand Down
3 changes: 2 additions & 1 deletion async-usercalls/src/provider_api.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
59 changes: 59 additions & 0 deletions async-usercalls/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,65 @@ fn safe_alloc_free() {

unsafe impl Send for MakeSend<StdUser<[u8]>> {}

#[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() {
Expand Down

0 comments on commit 1c64d6d

Please sign in to comment.