Skip to content

Commit

Permalink
update async-usercalls for new insecure time api
Browse files Browse the repository at this point in the history
  • Loading branch information
Taowyoo committed Oct 31, 2024
1 parent 8ac4c4f commit 688c2e9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
28 changes: 15 additions & 13 deletions intel-sgx/async-usercalls/src/callback.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use fortanix_sgx_abi::{invoke_with_usercalls, Fd, Result};
use fortanix_sgx_abi::{invoke_with_usercalls, Fd, Result, InsecureTimeInfo};
use std::io;
use std::os::fortanix_sgx::usercalls::raw::{Return, ReturnValue};
use std::os::fortanix_sgx::usercalls::FromSgxResult;
Expand All @@ -21,21 +21,23 @@ impl<F, T> From<F> for CbFn<T>
}

macro_rules! cbfn_type {
( ) => { CbFn<()> };
( -> ! ) => { () };
( -> u64 ) => { CbFn<u64> };
( -> (Result, usize) ) => { CbFn<io::Result<usize>> };
( -> (Result, u64) ) => { CbFn<io::Result<u64>> };
( -> (Result, Fd) ) => { CbFn<io::Result<Fd>> };
( -> (Result, *mut u8) ) => { CbFn<io::Result<*mut u8>> };
( -> Result ) => { CbFn<io::Result<()>> };
( ) => { CbFn<()> };
( -> ! ) => { () };
( -> u64 ) => { CbFn<u64> };
( -> (u64, *const InsecureTimeInfo) ) => { CbFn<(u64, *const InsecureTimeInfo)> };
( -> (Result, usize) ) => { CbFn<io::Result<usize>> };
( -> (Result, u64) ) => { CbFn<io::Result<u64>> };
( -> (Result, Fd) ) => { CbFn<io::Result<Fd>> };
( -> (Result, *mut u8) ) => { CbFn<io::Result<*mut u8>> };
( -> Result ) => { CbFn<io::Result<()>> };
}

macro_rules! call_cbfn {
( $cb:ident, $rv:expr, ) => { let x: () = $rv; $cb.call(x); };
( $cb:ident, $rv:expr, -> ! ) => { let _: ! = $rv; };
( $cb:ident, $rv:expr, -> u64 ) => { let x: u64 = $rv; $cb.call(x); };
( $cb:ident, $rv:expr, -> $t:ty ) => { let x: $t = $rv; $cb.call(x.from_sgx_result()); };
( $cb:ident, $rv:expr, ) => { let x: () = $rv; $cb.call(x); };
( $cb:ident, $rv:expr, -> ! ) => { let _: ! = $rv; };
( $cb:ident, $rv:expr, -> u64 ) => { let x: u64 = $rv; $cb.call(x); };
( $cb:ident, $rv:expr, -> (u64, *const InsecureTimeInfo) ) => { let x: (u64, *const InsecureTimeInfo) = $rv; $cb.call(x); };
( $cb:ident, $rv:expr, -> $t:ty ) => { let x: $t = $rv; $cb.call(x.from_sgx_result()); };
}

macro_rules! define_callback {
Expand Down
14 changes: 10 additions & 4 deletions intel-sgx/async-usercalls/src/provider_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::io_bufs::UserBuf;
use crate::raw::RawApi;
use crate::utils::MakeSend;
use crate::{AsyncUsercallProvider, CancelHandle};
use fortanix_sgx_abi::Fd;
use fortanix_sgx_abi::{Fd, InsecureTimeInfo};
use std::io;
use std::mem::{self, ManuallyDrop};
use std::net::{TcpListener, TcpStream};
Expand Down Expand Up @@ -251,11 +251,17 @@ impl AsyncUsercallProvider {
/// callbacks.
pub fn insecure_time<F>(&self, callback: F)
where
F: FnOnce(SystemTime) + Send + 'static,
F: FnOnce((SystemTime, Option<InsecureTimeInfo>)) + Send + 'static,
{
let cb = move |nanos_since_epoch| {
let cb = move |(nanos_since_epoch, insecure_time_info_ptr): (u64, *const InsecureTimeInfo)| {
let t = UNIX_EPOCH + Duration::from_nanos(nanos_since_epoch);
callback(t);
let maybe_insecure_time_info = if insecure_time_info_ptr.is_null() {
// Safety: The caller must ensure that ptr is non-null and valid.
None
} else {
unsafe { Some(*insecure_time_info_ptr) }
};
callback((t, maybe_insecure_time_info));
};
unsafe {
self.raw_insecure_time(Some(cb.into()));
Expand Down
8 changes: 4 additions & 4 deletions intel-sgx/async-usercalls/src/raw.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::callback::*;
use crate::{AsyncUsercallProvider, CancelHandle};
use fortanix_sgx_abi::Fd;
use fortanix_sgx_abi::{Fd, InsecureTimeInfo};
use std::io;
use std::os::fortanix_sgx::usercalls::raw::ByteBuffer;
use std::os::fortanix_sgx::usercalls::raw::{Usercall, UsercallNrs};
Expand Down Expand Up @@ -51,7 +51,7 @@ pub trait RawApi {
callback: Option<CbFn<io::Result<Fd>>>,
) -> CancelHandle;

unsafe fn raw_insecure_time(&self, callback: Option<CbFn<u64>>);
unsafe fn raw_insecure_time(&self, callback: Option<CbFn<(u64, *const InsecureTimeInfo)>>);

unsafe fn raw_alloc(&self, size: usize, alignment: usize, callback: Option<CbFn<io::Result<*mut u8>>>);

Expand Down Expand Up @@ -137,7 +137,7 @@ impl RawApi for AsyncUsercallProvider {
self.send_usercall(u, callback.map(|cb| Callback::connect_stream(cb)))
}

unsafe fn raw_insecure_time(&self, callback: Option<CbFn<u64>>) {
unsafe fn raw_insecure_time(&self, callback: Option<CbFn<(u64, *const InsecureTimeInfo)>>) {
let u = Usercall(UsercallNrs::insecure_time as _, 0, 0, 0, 0);
self.send_usercall(u, callback.map(|cb| Callback::insecure_time(cb)));
}
Expand Down Expand Up @@ -172,7 +172,7 @@ mod tests {
let (tx, rx) = mpmc::bounded(N);
for _ in 0..N {
let tx = tx.clone();
let cb = move |d| {
let cb = move |(d, _)| {
let system_time = UNIX_EPOCH + Duration::from_nanos(d);
tx.send(system_time).unwrap();
};
Expand Down

0 comments on commit 688c2e9

Please sign in to comment.