Skip to content

Commit

Permalink
Rewrite to handle EOF.
Browse files Browse the repository at this point in the history
  • Loading branch information
egrimley-arm committed Dec 21, 2022
1 parent f6079a8 commit e91c9b9
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 151 deletions.
21 changes: 8 additions & 13 deletions tests/tests/server_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ fn basic_program_read_non_existent() {
.unwrap();
}

//xx#[test]
#[test]
/// A client attempts to use an unauthorized key
fn basic_unauthorized_key() {
let events = vec![
Expand All @@ -235,7 +235,7 @@ fn basic_unauthorized_key() {
assert!(result.is_err(), "An error should occur");
}

//xx#[test]
#[test]
/// A client attempts to use an unauthorized certificate
fn basic_unauthorized_certificate() {
let events = vec![
Expand All @@ -256,7 +256,7 @@ fn basic_unauthorized_certificate() {
assert!(result.is_err(), "An error should occur");
}

//xx#[test]
#[test]
/// A unauthorized client attempts to connect the service
fn basic_unauthorized_certificate_key_pair() {
let events = vec![
Expand Down Expand Up @@ -735,32 +735,27 @@ impl TestExecutor {

let mut veracruz_session_clone = veracruz_session.clone();
let test_alive_flag_clone = test_alive_flag.clone();
let h1 = thread::spawn(move || {
thread::spawn(move || {
while test_alive_flag_clone.load(Ordering::SeqCst) {
let received = receiver.recv();
let received_buffer = received.map_err(|e| anyhow!("Server: {:?}", e)).unwrap();
info!(
"Server: receive {} byte(s).",
received_buffer.len(),
);
veracruz_session_clone.write_all(&received_buffer).unwrap();
}
});

let mut veracruz_session_clone = veracruz_session.clone();
let test_alive_flag_clone = test_alive_flag.clone();
let h2 = thread::spawn(move || {
thread::spawn(move || {
while test_alive_flag_clone.load(Ordering::SeqCst) {
let mut buf = vec![0; 1000];
let n = veracruz_session_clone.read(&mut buf).unwrap();
if n == 0 {
break;
}
sender.send(buf[0..n].to_vec()).unwrap();
}
});

/*
h1.join().unwrap();
h2.join().unwrap();
*/
Ok(())
}

Expand Down
105 changes: 59 additions & 46 deletions veracruz-server/src/platforms/icecap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use crate::common::{VeracruzServerError, VeracruzServerResult};
use err_derive::Error;
use log::error;
use policy_utils::policy::Policy;
use proxy_attestation_client;
use signal_hook::{
Expand Down Expand Up @@ -407,15 +408,26 @@ impl Drop for VeracruzServerIceCap {
}
}

type EnclaveHandler = Arc<Mutex<Option<VeracruzServerIceCap>>>;
////////////////////////////////////////////////////////////////////////////////

pub struct VeracruzServer(EnclaveHandler);
//xx This should perhaps be called VeracruzEnclave?
pub struct VeracruzServer(
Arc<(Mutex<Option<VeracruzServerIceCap>>, Condvar)>
);

//xx This should perhaps be called VeracruzConnection?
pub struct VeracruzSession {
enclave: VeracruzServer,
session_id: u32,
buffer: Arc<Mutex<Vec<u8>>>,
}

impl VeracruzServer {
pub fn new(policy: &str) -> VeracruzServerResult<Self> {
Ok(VeracruzServer(Arc::new(Mutex::new(Some(
VeracruzServerIceCap::new(policy)?,
)))))
Ok(VeracruzServer(Arc::new((
Mutex::new(Some(VeracruzServerIceCap::new(policy)?)),
Condvar::new(),
))))
}
pub fn clone(&self) -> Self {
VeracruzServer(self.0.clone())
Expand All @@ -425,21 +437,17 @@ impl VeracruzServer {
enclave: VeracruzServer(self.0.clone()),
session_id: self
.0
.lock()?
.0
.lock()
.unwrap()
.as_mut()
.ok_or(VeracruzServerError::UninitializedEnclaveError)?
.new_tls_session()?,
buffer: Arc::new((Mutex::new(vec![]), Condvar::new())),
buffer: Arc::new(Mutex::new(vec![])),
})
}
}

pub struct VeracruzSession {
enclave: VeracruzServer,
session_id: u32,
buffer: Arc<(Mutex<Vec<u8>>, Condvar)>,
}

impl VeracruzSession {
pub fn clone(&self) -> Self {
VeracruzSession {
Expand All @@ -455,49 +463,54 @@ impl Read for VeracruzSession {
if buf.len() == 0 {
Ok(0)
} else {
let mut buffer = self.buffer.0.lock().unwrap();
while buffer.len() == 0 {
buffer = self.buffer.1.wait(buffer).unwrap();
let mut enclave = self.enclave.0.0.lock().unwrap();
loop {
{
let mut buffer = self.buffer.lock().unwrap();
if enclave.is_none() || buffer.len() > 0 {
let n = std::cmp::min(buf.len(), buffer.len());
buf[0..n].clone_from_slice(&buffer[0..n]);
buffer.drain(0..n);
return Ok(n);
}
}
enclave = self.enclave.0.1.wait(enclave).unwrap();
}
let n = std::cmp::min(buf.len(), buffer.len());
buf[0..n].clone_from_slice(&buffer[0..n]);
buffer.drain(0..n);
Ok(n)
}
}
}

impl Write for VeracruzSession {
fn write(&mut self, buf: &[u8]) -> std::result::Result<usize, std::io::Error> {
if buf.len() > 0 {
let (active, output) = self
.enclave
.0
.lock()
.map_err(|_| std::io::Error::new(std::io::ErrorKind::Other, "xx"))?
.as_mut()
.ok_or(std::io::Error::new(std::io::ErrorKind::Other, "xx"))?
.tls_data(self.session_id, buf.to_vec())
.map_err(|_| std::io::Error::new(std::io::ErrorKind::Other, "xx"))?;
if !active {
let mut mb_enclave = self
.enclave
.0
.lock()
.map_err(|_| std::io::Error::new(std::io::ErrorKind::Other, "xx"))?;
*mb_enclave = None;
}
let mut buffer = self
.buffer
.0
.lock()
.map_err(|_| std::io::Error::new(std::io::ErrorKind::Other, "xx"))?;
for x1 in output {
for mut x in x1 {
buffer.append(&mut x);
let mut mb_enclave = self.enclave.0.0.lock().unwrap();
match mb_enclave.as_mut() {
None => return Ok(0),
Some(enclave) => {
let (active, output) =
match enclave.tls_data(self.session_id, buf.to_vec()) {
Ok(x) => x,
Err(e) => {
error!("tls_data gave error: {}", e);
(false, None)
}
};
if !active {
eprintln!("session write: !active");
mb_enclave.take();
}
let mut buffer = self.buffer.lock().unwrap();
let buffer_len = buffer.len();
for x1 in output {
for mut x in x1 {
buffer.append(&mut x);
}
}
if !active || (buffer_len == 0 && buf.len() > 0) {
self.enclave.0.1.notify_all();
}
}
}
self.buffer.1.notify_one();
}
Ok(buf.len())
}
Expand Down
104 changes: 58 additions & 46 deletions veracruz-server/src/platforms/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,15 +547,26 @@
}
}

type EnclaveHandler = Arc<Mutex<Option<VeracruzServerLinux>>>;
////////////////////////////////////////////////////////////////////////////////

pub struct VeracruzServer(EnclaveHandler);
//xx This should perhaps be called VeracruzEnclave?
pub struct VeracruzServer(
Arc<(Mutex<Option<VeracruzServerLinux>>, Condvar)>
);

//xx This should perhaps be called VeracruzConnection?
pub struct VeracruzSession {
enclave: VeracruzServer,
session_id: u32,
buffer: Arc<Mutex<Vec<u8>>>,
}

impl VeracruzServer {
pub fn new(policy: &str) -> VeracruzServerResult<Self> {
Ok(VeracruzServer(Arc::new(Mutex::new(Some(
VeracruzServerLinux::new(policy)?,
)))))
Ok(VeracruzServer(Arc::new((
Mutex::new(Some(VeracruzServerLinux::new(policy)?)),
Condvar::new(),
))))
}
pub fn clone(&self) -> Self {
VeracruzServer(self.0.clone())
Expand All @@ -565,21 +576,17 @@ impl VeracruzServer {
enclave: VeracruzServer(self.0.clone()),
session_id: self
.0
.lock()?
.0
.lock()
.unwrap()
.as_mut()
.ok_or(VeracruzServerError::UninitializedEnclaveError)?
.new_tls_session()?,
buffer: Arc::new((Mutex::new(vec![]), Condvar::new())),
buffer: Arc::new(Mutex::new(vec![])),
})
}
}

pub struct VeracruzSession {
enclave: VeracruzServer,
session_id: u32,
buffer: Arc<(Mutex<Vec<u8>>, Condvar)>,
}

impl VeracruzSession {
pub fn clone(&self) -> Self {
VeracruzSession {
Expand All @@ -595,49 +602,54 @@ impl Read for VeracruzSession {
if buf.len() == 0 {
Ok(0)
} else {
let mut buffer = self.buffer.0.lock().unwrap();
while buffer.len() == 0 {
buffer = self.buffer.1.wait(buffer).unwrap();
let mut enclave = self.enclave.0.0.lock().unwrap();
loop {
{
let mut buffer = self.buffer.lock().unwrap();
if enclave.is_none() || buffer.len() > 0 {
let n = std::cmp::min(buf.len(), buffer.len());
buf[0..n].clone_from_slice(&buffer[0..n]);
buffer.drain(0..n);
return Ok(n);
}
}
enclave = self.enclave.0.1.wait(enclave).unwrap();
}
let n = std::cmp::min(buf.len(), buffer.len());
buf[0..n].clone_from_slice(&buffer[0..n]);
buffer.drain(0..n);
Ok(n)
}
}
}

impl Write for VeracruzSession {
fn write(&mut self, buf: &[u8]) -> std::result::Result<usize, std::io::Error> {
if buf.len() > 0 {
let (active, output) = self
.enclave
.0
.lock()
.map_err(|_| std::io::Error::new(std::io::ErrorKind::Other, "xx"))?
.as_mut()
.ok_or(std::io::Error::new(std::io::ErrorKind::Other, "xx"))?
.tls_data(self.session_id, buf.to_vec())
.map_err(|_| std::io::Error::new(std::io::ErrorKind::Other, "xx"))?;
if !active {
let mut mb_enclave = self
.enclave
.0
.lock()
.map_err(|_| std::io::Error::new(std::io::ErrorKind::Other, "xx"))?;
*mb_enclave = None;
}
let mut buffer = self
.buffer
.0
.lock()
.map_err(|_| std::io::Error::new(std::io::ErrorKind::Other, "xx"))?;
for x1 in output {
for mut x in x1 {
buffer.append(&mut x);
let mut mb_enclave = self.enclave.0.0.lock().unwrap();
match mb_enclave.as_mut() {
None => return Ok(0),
Some(enclave) => {
let (active, output) =
match enclave.tls_data(self.session_id, buf.to_vec()) {
Ok(x) => x,
Err(e) => {
error!("tls_data gave error: {}", e);
(false, None)
}
};
if !active {
eprintln!("session write: !active");
mb_enclave.take();
}
let mut buffer = self.buffer.lock().unwrap();
let buffer_len = buffer.len();
for x1 in output {
for mut x in x1 {
buffer.append(&mut x);
}
}
if !active || (buffer_len == 0 && buf.len() > 0) {
self.enclave.0.1.notify_all();
}
}
}
self.buffer.1.notify_one();
}
Ok(buf.len())
}
Expand Down
Loading

0 comments on commit e91c9b9

Please sign in to comment.