diff --git a/intel-sgx/async-usercalls/src/callback.rs b/intel-sgx/async-usercalls/src/callback.rs index ea5d969b..3194fa3d 100644 --- a/intel-sgx/async-usercalls/src/callback.rs +++ b/intel-sgx/async-usercalls/src/callback.rs @@ -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; @@ -21,21 +21,23 @@ impl From for CbFn } macro_rules! cbfn_type { - ( ) => { CbFn<()> }; - ( -> ! ) => { () }; - ( -> u64 ) => { CbFn }; - ( -> (Result, usize) ) => { CbFn> }; - ( -> (Result, u64) ) => { CbFn> }; - ( -> (Result, Fd) ) => { CbFn> }; - ( -> (Result, *mut u8) ) => { CbFn> }; - ( -> Result ) => { CbFn> }; + ( ) => { CbFn<()> }; + ( -> ! ) => { () }; + ( -> u64 ) => { CbFn }; + ( -> (u64, *const InsecureTimeInfo) ) => { CbFn<(u64, *const InsecureTimeInfo)> }; + ( -> (Result, usize) ) => { CbFn> }; + ( -> (Result, u64) ) => { CbFn> }; + ( -> (Result, Fd) ) => { CbFn> }; + ( -> (Result, *mut u8) ) => { CbFn> }; + ( -> Result ) => { CbFn> }; } 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 { diff --git a/intel-sgx/async-usercalls/src/provider_api.rs b/intel-sgx/async-usercalls/src/provider_api.rs index 304c4101..3ab70a29 100644 --- a/intel-sgx/async-usercalls/src/provider_api.rs +++ b/intel-sgx/async-usercalls/src/provider_api.rs @@ -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}; @@ -251,11 +251,17 @@ impl AsyncUsercallProvider { /// callbacks. pub fn insecure_time(&self, callback: F) where - F: FnOnce(SystemTime) + Send + 'static, + F: FnOnce((SystemTime, Option)) + 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())); diff --git a/intel-sgx/async-usercalls/src/raw.rs b/intel-sgx/async-usercalls/src/raw.rs index e66e5a35..ae511b16 100644 --- a/intel-sgx/async-usercalls/src/raw.rs +++ b/intel-sgx/async-usercalls/src/raw.rs @@ -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}; @@ -51,7 +51,7 @@ pub trait RawApi { callback: Option>>, ) -> CancelHandle; - unsafe fn raw_insecure_time(&self, callback: Option>); + unsafe fn raw_insecure_time(&self, callback: Option>); unsafe fn raw_alloc(&self, size: usize, alignment: usize, callback: Option>>); @@ -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>) { + unsafe fn raw_insecure_time(&self, callback: Option>) { let u = Usercall(UsercallNrs::insecure_time as _, 0, 0, 0, 0); self.send_usercall(u, callback.map(|cb| Callback::insecure_time(cb))); } @@ -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(); };