about summary refs log tree commit diff
path: root/src/libstd/sys/sgx
diff options
context:
space:
mode:
authorMark Rousskov <mark.simulacrum@gmail.com>2019-12-22 17:42:04 -0500
committerMark Rousskov <mark.simulacrum@gmail.com>2019-12-22 17:42:47 -0500
commita06baa56b95674fc626b3c3fd680d6a65357fe60 (patch)
treecd9d867c2ca3cff5c1d6b3bd73377c44649fb075 /src/libstd/sys/sgx
parent8eb7c58dbb7b32701af113bc58722d0d1fefb1eb (diff)
Format the world
Diffstat (limited to 'src/libstd/sys/sgx')
-rw-r--r--src/libstd/sys/sgx/condvar.rs2
-rw-r--r--src/libstd/sys/sgx/mod.rs16
-rw-r--r--src/libstd/sys/sgx/mutex.rs22
-rw-r--r--src/libstd/sys/sgx/rwlock.rs41
-rw-r--r--src/libstd/sys/sgx/waitqueue.rs115
5 files changed, 75 insertions, 121 deletions
diff --git a/src/libstd/sys/sgx/condvar.rs b/src/libstd/sys/sgx/condvar.rs
index cc1c04a83e7..9c5c086184d 100644
--- a/src/libstd/sys/sgx/condvar.rs
+++ b/src/libstd/sys/sgx/condvar.rs
@@ -1,7 +1,7 @@
 use crate::sys::mutex::Mutex;
 use crate::time::Duration;
 
-use super::waitqueue::{WaitVariable, WaitQueue, SpinMutex};
+use super::waitqueue::{SpinMutex, WaitQueue, WaitVariable};
 
 pub struct Condvar {
     inner: SpinMutex<WaitVariable<()>>,
diff --git a/src/libstd/sys/sgx/mod.rs b/src/libstd/sys/sgx/mod.rs
index 601957acd5c..83cee0cf35a 100644
--- a/src/libstd/sys/sgx/mod.rs
+++ b/src/libstd/sys/sgx/mod.rs
@@ -28,16 +28,15 @@ pub mod pipe;
 pub mod process;
 pub mod rwlock;
 pub mod stack_overflow;
+pub mod stdio;
 pub mod thread;
 pub mod thread_local;
 pub mod time;
-pub mod stdio;
 
 pub use crate::sys_common::os_str_bytes as os_str;
 
 #[cfg(not(test))]
-pub fn init() {
-}
+pub fn init() {}
 
 /// This function is used to implement functionality that simply doesn't exist.
 /// Programs relying on this functionality will need to deal with the error.
@@ -46,8 +45,7 @@ pub fn unsupported<T>() -> crate::io::Result<T> {
 }
 
 pub fn unsupported_err() -> crate::io::Error {
-    crate::io::Error::new(ErrorKind::Other,
-                   "operation not supported on SGX yet")
+    crate::io::Error::new(ErrorKind::Other, "operation not supported on SGX yet")
 }
 
 /// This function is used to implement various functions that doesn't exist,
@@ -58,8 +56,10 @@ pub fn unsupported_err() -> crate::io::Error {
 pub fn sgx_ineffective<T>(v: T) -> crate::io::Result<T> {
     static SGX_INEFFECTIVE_ERROR: AtomicBool = AtomicBool::new(false);
     if SGX_INEFFECTIVE_ERROR.load(Ordering::Relaxed) {
-        Err(crate::io::Error::new(ErrorKind::Other,
-                       "operation can't be trusted to have any effect on SGX"))
+        Err(crate::io::Error::new(
+            ErrorKind::Other,
+            "operation can't be trusted to have any effect on SGX",
+        ))
     } else {
         Ok(v)
     }
@@ -121,7 +121,7 @@ pub unsafe fn strlen(mut s: *const c_char) -> usize {
         n += 1;
         s = s.offset(1);
     }
-    return n
+    return n;
 }
 
 pub unsafe fn abort_internal() -> ! {
diff --git a/src/libstd/sys/sgx/mutex.rs b/src/libstd/sys/sgx/mutex.rs
index 662da8b3f66..eebbea1b285 100644
--- a/src/libstd/sys/sgx/mutex.rs
+++ b/src/libstd/sys/sgx/mutex.rs
@@ -2,7 +2,7 @@ use fortanix_sgx_abi::Tcs;
 
 use super::abi::thread;
 
-use super::waitqueue::{WaitVariable, WaitQueue, SpinMutex, NotifiedTcs, try_lock_or_false};
+use super::waitqueue::{try_lock_or_false, NotifiedTcs, SpinMutex, WaitQueue, WaitVariable};
 
 pub struct Mutex {
     inner: SpinMutex<WaitVariable<bool>>,
@@ -22,8 +22,8 @@ impl Mutex {
         let mut guard = self.inner.lock();
         if *guard.lock_var() {
             // Another thread has the lock, wait
-            WaitQueue::wait(guard, ||{})
-            // Another thread has passed the lock to us
+            WaitQueue::wait(guard, || {})
+        // Another thread has passed the lock to us
         } else {
             // We are just now obtaining the lock
             *guard.lock_var_mut() = true;
@@ -60,7 +60,7 @@ impl Mutex {
 
 struct ReentrantLock {
     owner: Option<Tcs>,
-    count: usize
+    count: usize,
 }
 
 pub struct ReentrantMutex {
@@ -70,7 +70,7 @@ pub struct ReentrantMutex {
 impl ReentrantMutex {
     pub const fn uninitialized() -> ReentrantMutex {
         ReentrantMutex {
-            inner: SpinMutex::new(WaitVariable::new(ReentrantLock { owner: None, count: 0 }))
+            inner: SpinMutex::new(WaitVariable::new(ReentrantLock { owner: None, count: 0 })),
         }
     }
 
@@ -83,14 +83,14 @@ impl ReentrantMutex {
         match guard.lock_var().owner {
             Some(tcs) if tcs != thread::current() => {
                 // Another thread has the lock, wait
-                WaitQueue::wait(guard, ||{});
+                WaitQueue::wait(guard, || {});
                 // Another thread has passed the lock to us
-            },
+            }
             _ => {
                 // We are just now obtaining the lock
                 guard.lock_var_mut().owner = Some(thread::current());
                 guard.lock_var_mut().count += 1;
-            },
+            }
         }
     }
 
@@ -105,7 +105,7 @@ impl ReentrantMutex {
                     // No other waiters, unlock
                     guard.lock_var_mut().count = 0;
                     guard.lock_var_mut().owner = None;
-                },
+                }
                 Ok(mut guard) => {
                     // There was a thread waiting, just pass the lock
                     if let NotifiedTcs::Single(tcs) = guard.notified_tcs() {
@@ -125,13 +125,13 @@ impl ReentrantMutex {
             Some(tcs) if tcs != thread::current() => {
                 // Another thread has the lock
                 false
-            },
+            }
             _ => {
                 // We are just now obtaining the lock
                 guard.lock_var_mut().owner = Some(thread::current());
                 guard.lock_var_mut().count += 1;
                 true
-            },
+            }
         }
     }
 
diff --git a/src/libstd/sys/sgx/rwlock.rs b/src/libstd/sys/sgx/rwlock.rs
index e2f94b1d928..fda2bb504d4 100644
--- a/src/libstd/sys/sgx/rwlock.rs
+++ b/src/libstd/sys/sgx/rwlock.rs
@@ -31,8 +31,8 @@ impl RWLock {
         if *wguard.lock_var() || !wguard.queue_empty() {
             // Another thread has or is waiting for the write lock, wait
             drop(wguard);
-            WaitQueue::wait(rguard, ||{});
-            // Another thread has passed the lock to us
+            WaitQueue::wait(rguard, || {});
+        // Another thread has passed the lock to us
         } else {
             // No waiting writers, acquire the read lock
             *rguard.lock_var_mut() =
@@ -62,8 +62,8 @@ impl RWLock {
         if *wguard.lock_var() || rguard.lock_var().is_some() {
             // Another thread has the lock, wait
             drop(rguard);
-            WaitQueue::wait(wguard, ||{});
-            // Another thread has passed the lock to us
+            WaitQueue::wait(wguard, || {});
+        // Another thread has passed the lock to us
         } else {
             // We are just now obtaining the lock
             *wguard.lock_var_mut() = true;
@@ -133,7 +133,7 @@ impl RWLock {
                 } else {
                     // No readers waiting, the lock is released
                 }
-            },
+            }
             Ok(wguard) => {
                 // There was a thread waiting for write, just pass the lock
                 wguard.drop_after(rguard);
@@ -202,8 +202,8 @@ pub unsafe extern "C" fn __rust_rwlock_unlock(p: *mut RWLock) -> i32 {
 #[cfg(test)]
 mod tests {
     use super::*;
-    use core::array::FixedSizeArray;
     use crate::mem::{self, MaybeUninit};
+    use core::array::FixedSizeArray;
 
     // Verify that the bytes of initialized RWLock are the same as in
     // libunwind. If they change, `src/UnwindRustSgx.h` in libunwind needs to
@@ -211,22 +211,14 @@ mod tests {
     #[test]
     fn test_c_rwlock_initializer() {
         const RWLOCK_INIT: &[u8] = &[
-            0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
-            0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
-            0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
-            0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
-            0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
-            0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
-            0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
-            0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
-            0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
-            0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
-            0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
-            0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
-            0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
-            0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
-            0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
-            0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
+            0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
+            0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0,
+            0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
+            0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
+            0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
+            0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0,
+            0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
+            0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
         ];
 
         #[inline(never)]
@@ -247,10 +239,7 @@ mod tests {
             zero_stack();
             let mut init = MaybeUninit::<RWLock>::zeroed();
             rwlock_new(&mut init);
-            assert_eq!(
-                mem::transmute::<_, [u8; 128]>(init.assume_init()).as_slice(),
-                RWLOCK_INIT
-            )
+            assert_eq!(mem::transmute::<_, [u8; 128]>(init.assume_init()).as_slice(), RWLOCK_INIT)
         };
     }
 }
diff --git a/src/libstd/sys/sgx/waitqueue.rs b/src/libstd/sys/sgx/waitqueue.rs
index 3cb40e509b6..6e50f161b3b 100644
--- a/src/libstd/sys/sgx/waitqueue.rs
+++ b/src/libstd/sys/sgx/waitqueue.rs
@@ -1,3 +1,4 @@
+use crate::num::NonZeroUsize;
 /// A simple queue implementation for synchronization primitives.
 ///
 /// This queue is used to implement condition variable and mutexes.
@@ -9,23 +10,21 @@
 /// Since userspace may send spurious wake-ups, the wakeup event state is
 /// recorded in the enclave. The wakeup event state is protected by a spinlock.
 /// The queue and associated wait state are stored in a `WaitVariable`.
-
 use crate::ops::{Deref, DerefMut};
-use crate::num::NonZeroUsize;
 
-use fortanix_sgx_abi::{Tcs, EV_UNPARK, WAIT_INDEFINITE};
-use super::abi::usercalls;
 use super::abi::thread;
+use super::abi::usercalls;
+use fortanix_sgx_abi::{Tcs, EV_UNPARK, WAIT_INDEFINITE};
 
+pub use self::spin_mutex::{try_lock_or_false, SpinMutex, SpinMutexGuard};
 use self::unsafe_list::{UnsafeList, UnsafeListEntry};
-pub use self::spin_mutex::{SpinMutex, SpinMutexGuard, try_lock_or_false};
 
 /// An queue entry in a `WaitQueue`.
 struct WaitEntry {
     /// TCS address of the thread that is waiting
     tcs: Tcs,
     /// Whether this thread has been notified to be awoken
-    wake: bool
+    wake: bool,
 }
 
 /// Data stored with a `WaitQueue` alongside it. This ensures accesses to the
@@ -36,15 +35,12 @@ struct WaitEntry {
 #[derive(Default)]
 pub struct WaitVariable<T> {
     queue: WaitQueue,
-    lock: T
+    lock: T,
 }
 
 impl<T> WaitVariable<T> {
     pub const fn new(var: T) -> Self {
-        WaitVariable {
-            queue: WaitQueue::new(),
-            lock: var
-        }
+        WaitVariable { queue: WaitQueue::new(), lock: var }
     }
 
     pub fn queue_empty(&self) -> bool {
@@ -63,14 +59,14 @@ impl<T> WaitVariable<T> {
 #[derive(Copy, Clone)]
 pub enum NotifiedTcs {
     Single(Tcs),
-    All { count: NonZeroUsize }
+    All { count: NonZeroUsize },
 }
 
 /// An RAII guard that will notify a set of target threads as well as unlock
 /// a mutex on drop.
 pub struct WaitGuard<'a, T: 'a> {
     mutex_guard: Option<SpinMutexGuard<'a, WaitVariable<T>>>,
-    notified_tcs: NotifiedTcs
+    notified_tcs: NotifiedTcs,
 }
 
 /// A queue of threads that are waiting on some synchronization primitive.
@@ -125,7 +121,7 @@ impl<'a, T> Drop for WaitGuard<'a, T> {
         drop(self.mutex_guard.take());
         let target_tcs = match self.notified_tcs {
             NotifiedTcs::Single(tcs) => Some(tcs),
-            NotifiedTcs::All { .. } => None
+            NotifiedTcs::All { .. } => None,
         };
         rtunwrap!(Ok, usercalls::send(EV_UNPARK, target_tcs));
     }
@@ -133,9 +129,7 @@ impl<'a, T> Drop for WaitGuard<'a, T> {
 
 impl WaitQueue {
     pub const fn new() -> Self {
-        WaitQueue {
-            inner: UnsafeList::new()
-        }
+        WaitQueue { inner: UnsafeList::new() }
     }
 
     pub fn is_empty(&self) -> bool {
@@ -151,7 +145,7 @@ impl WaitQueue {
         unsafe {
             let mut entry = UnsafeListEntry::new(SpinMutex::new(WaitEntry {
                 tcs: thread::current(),
-                wake: false
+                wake: false,
             }));
             let entry = guard.queue.inner.push(&mut entry);
             drop(guard);
@@ -169,19 +163,16 @@ impl WaitQueue {
     ///
     /// If a waiter is found, a `WaitGuard` is returned which will notify the
     /// waiter when it is dropped.
-    pub fn notify_one<T>(mut guard: SpinMutexGuard<'_, WaitVariable<T>>)
-        -> Result<WaitGuard<'_, T>, SpinMutexGuard<'_, WaitVariable<T>>>
-    {
+    pub fn notify_one<T>(
+        mut guard: SpinMutexGuard<'_, WaitVariable<T>>,
+    ) -> Result<WaitGuard<'_, T>, SpinMutexGuard<'_, WaitVariable<T>>> {
         unsafe {
             if let Some(entry) = guard.queue.inner.pop() {
                 let mut entry_guard = entry.lock();
                 let tcs = entry_guard.tcs;
                 entry_guard.wake = true;
                 drop(entry);
-                Ok(WaitGuard {
-                    mutex_guard: Some(guard),
-                    notified_tcs: NotifiedTcs::Single(tcs)
-                })
+                Ok(WaitGuard { mutex_guard: Some(guard), notified_tcs: NotifiedTcs::Single(tcs) })
             } else {
                 Err(guard)
             }
@@ -193,9 +184,9 @@ impl WaitQueue {
     ///
     /// If at least one waiter is found, a `WaitGuard` is returned which will
     /// notify all waiters when it is dropped.
-    pub fn notify_all<T>(mut guard: SpinMutexGuard<'_, WaitVariable<T>>)
-        -> Result<WaitGuard<'_, T>, SpinMutexGuard<'_, WaitVariable<T>>>
-    {
+    pub fn notify_all<T>(
+        mut guard: SpinMutexGuard<'_, WaitVariable<T>>,
+    ) -> Result<WaitGuard<'_, T>, SpinMutexGuard<'_, WaitVariable<T>>> {
         unsafe {
             let mut count = 0;
             while let Some(entry) = guard.queue.inner.pop() {
@@ -204,10 +195,7 @@ impl WaitQueue {
                 entry_guard.wake = true;
             }
             if let Some(count) = NonZeroUsize::new(count) {
-                Ok(WaitGuard {
-                    mutex_guard: Some(guard),
-                    notified_tcs: NotifiedTcs::All { count }
-                })
+                Ok(WaitGuard { mutex_guard: Some(guard), notified_tcs: NotifiedTcs::All { count } })
             } else {
                 Err(guard)
             }
@@ -218,29 +206,22 @@ impl WaitQueue {
 /// A doubly-linked list where callers are in charge of memory allocation
 /// of the nodes in the list.
 mod unsafe_list {
-    use crate::ptr::NonNull;
     use crate::mem;
+    use crate::ptr::NonNull;
 
     pub struct UnsafeListEntry<T> {
         next: NonNull<UnsafeListEntry<T>>,
         prev: NonNull<UnsafeListEntry<T>>,
-        value: Option<T>
+        value: Option<T>,
     }
 
     impl<T> UnsafeListEntry<T> {
         fn dummy() -> Self {
-            UnsafeListEntry {
-                next: NonNull::dangling(),
-                prev: NonNull::dangling(),
-                value: None
-            }
+            UnsafeListEntry { next: NonNull::dangling(), prev: NonNull::dangling(), value: None }
         }
 
         pub fn new(value: T) -> Self {
-            UnsafeListEntry {
-                value: Some(value),
-                ..Self::dummy()
-            }
+            UnsafeListEntry { value: Some(value), ..Self::dummy() }
         }
     }
 
@@ -252,10 +233,7 @@ mod unsafe_list {
     impl<T> UnsafeList<T> {
         pub const fn new() -> Self {
             unsafe {
-                UnsafeList {
-                    head_tail: NonNull::new_unchecked(1 as _),
-                    head_tail_entry: None
-                }
+                UnsafeList { head_tail: NonNull::new_unchecked(1 as _), head_tail_entry: None }
             }
         }
 
@@ -416,8 +394,8 @@ mod unsafe_list {
 // FIXME: Perhaps use Intel TSX to avoid locking?
 mod spin_mutex {
     use crate::cell::UnsafeCell;
-    use crate::sync::atomic::{AtomicBool, Ordering, spin_loop_hint};
     use crate::ops::{Deref, DerefMut};
+    use crate::sync::atomic::{spin_loop_hint, AtomicBool, Ordering};
 
     #[derive(Default)]
     pub struct SpinMutex<T> {
@@ -437,20 +415,19 @@ mod spin_mutex {
 
     impl<T> SpinMutex<T> {
         pub const fn new(value: T) -> Self {
-            SpinMutex {
-                value: UnsafeCell::new(value),
-                lock: AtomicBool::new(false)
-            }
+            SpinMutex { value: UnsafeCell::new(value), lock: AtomicBool::new(false) }
         }
 
         #[inline(always)]
         pub fn lock(&self) -> SpinMutexGuard<'_, T> {
             loop {
                 match self.try_lock() {
-                    None => while self.lock.load(Ordering::Relaxed) {
-                        spin_loop_hint()
-                    },
-                    Some(guard) => return guard
+                    None => {
+                        while self.lock.load(Ordering::Relaxed) {
+                            spin_loop_hint()
+                        }
+                    }
+                    Some(guard) => return guard,
                 }
             }
         }
@@ -458,9 +435,7 @@ mod spin_mutex {
         #[inline(always)]
         pub fn try_lock(&self) -> Option<SpinMutexGuard<'_, T>> {
             if !self.lock.compare_and_swap(false, true, Ordering::Acquire) {
-                Some(SpinMutexGuard {
-                    mutex: self,
-                })
+                Some(SpinMutexGuard { mutex: self })
             } else {
                 None
             }
@@ -468,31 +443,21 @@ mod spin_mutex {
     }
 
     /// Lock the Mutex or return false.
-    pub macro try_lock_or_false {
-        ($e:expr) => {
-            if let Some(v) = $e.try_lock() {
-                v
-            } else {
-                return false
-            }
-        }
+    pub macro try_lock_or_false($e:expr) {
+        if let Some(v) = $e.try_lock() { v } else { return false }
     }
 
     impl<'a, T> Deref for SpinMutexGuard<'a, T> {
         type Target = T;
 
         fn deref(&self) -> &T {
-            unsafe {
-                &*self.mutex.value.get()
-            }
+            unsafe { &*self.mutex.value.get() }
         }
     }
 
     impl<'a, T> DerefMut for SpinMutexGuard<'a, T> {
         fn deref_mut(&mut self) -> &mut T {
-            unsafe {
-                &mut*self.mutex.value.get()
-            }
+            unsafe { &mut *self.mutex.value.get() }
         }
     }
 
@@ -509,7 +474,7 @@ mod spin_mutex {
         use super::*;
         use crate::sync::Arc;
         use crate::thread;
-        use crate::time::{SystemTime, Duration};
+        use crate::time::{Duration, SystemTime};
 
         #[test]
         fn sleep() {
@@ -552,7 +517,7 @@ mod tests {
             assert!(WaitQueue::notify_one(wq2.lock()).is_ok());
         });
 
-        WaitQueue::wait(locked, ||{});
+        WaitQueue::wait(locked, || {});
 
         t1.join().unwrap();
     }