Merge pull request #279 from apache/support-new-allocator

compiler: bump up to nightly-2020-10-25 for new AllocRef trait and AllocError
diff --git a/rust-toolchain b/rust-toolchain
index 641cabd..148ed93 100644
--- a/rust-toolchain
+++ b/rust-toolchain
@@ -1 +1 @@
-nightly-2020-09-10
+nightly-2020-10-25
diff --git a/samplecode/switchless/app/src/main.rs b/samplecode/switchless/app/src/main.rs
index 4de1afd..8876218 100644
--- a/samplecode/switchless/app/src/main.rs
+++ b/samplecode/switchless/app/src/main.rs
@@ -16,6 +16,7 @@
 // under the License..
 
 #![feature(link_args)]
+#![allow(unused_attributes)]
 #![link_args = "-Wl,--whole-archive -lsgx_uswitchless -Wl,--no-whole-archive"]
 
 extern crate sgx_types;
diff --git a/sgx_alloc/src/alignalloc.rs b/sgx_alloc/src/alignalloc.rs
index f96aaa5..6d5460a 100644
--- a/sgx_alloc/src/alignalloc.rs
+++ b/sgx_alloc/src/alignalloc.rs
@@ -33,18 +33,18 @@
 
 impl AlignAlloc {
     #[inline]
-    pub unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AlighAllocErr> {
+    pub unsafe fn alloc(&self, layout: Layout) -> Result<NonNull<u8>, AlighAllocErr> {
         NonNull::new(platform::alloc(layout)).ok_or(AlighAllocErr)
     }
 
     #[inline]
-    pub unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<u8>, AlighAllocErr> {
+    pub unsafe fn alloc_zeroed(&self, layout: Layout) -> Result<NonNull<u8>, AlighAllocErr> {
         NonNull::new(platform::alloc_zeroed(layout)).ok_or(AlighAllocErr)
     }
 
     #[inline]
     pub unsafe fn alloc_with_req(
-        &mut self,
+        &self,
         layout: Layout,
         align_req: &[AlignReq],
     ) -> Result<NonNull<u8>, AlighAllocErr> {
@@ -53,7 +53,7 @@
 
     #[inline]
     pub unsafe fn alloc_with_req_zeroed(
-        &mut self,
+        &self,
         layout: Layout,
         align_req: &[AlignReq],
     ) -> Result<NonNull<u8>, AlighAllocErr> {
@@ -62,7 +62,7 @@
 
     #[inline]
     pub unsafe fn alloc_with_pad_align(
-        &mut self,
+        &self,
         layout: Layout,
         align_layout: Layout,
     ) -> Result<NonNull<u8>, AlighAllocErr> {
@@ -71,7 +71,7 @@
 
     #[inline]
     pub unsafe fn alloc_with_pad_align_zeroed(
-        &mut self,
+        &self,
         layout: Layout,
         align_layout: Layout,
     ) -> Result<NonNull<u8>, AlighAllocErr> {
@@ -80,7 +80,7 @@
     }
 
     #[inline]
-    pub unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
+    pub unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
         platform::dealloc(ptr.as_ptr(), layout)
     }
 
diff --git a/sgx_alloc/src/rsrvmem.rs b/sgx_alloc/src/rsrvmem.rs
index 7248637..2cf832c 100644
--- a/sgx_alloc/src/rsrvmem.rs
+++ b/sgx_alloc/src/rsrvmem.rs
@@ -44,7 +44,7 @@
     /// Starting address of the new allocated memory area on success;
     ///
     #[inline]
-    pub unsafe fn alloc(&mut self, count: u32) -> Result<NonNull<u8>, RsrvMemAllocErr> {
+    pub unsafe fn alloc(&self, count: u32) -> Result<NonNull<u8>, RsrvMemAllocErr> {
         NonNull::new(platform::alloc(count)).ok_or(RsrvMemAllocErr)
     }
 
@@ -65,12 +65,12 @@
     /// Starting address of the new allocated memory area on success;
     ///
     #[inline]
-    pub unsafe fn alloc_with_addr(&mut self, addr: NonNull<u8>, count: u32) -> Result<NonNull<u8>, RsrvMemAllocErr> {
+    pub unsafe fn alloc_with_addr(&self, addr: NonNull<u8>, count: u32) -> Result<NonNull<u8>, RsrvMemAllocErr> {
         NonNull::new(platform::alloc_with_addr(addr.as_ptr(), count)).ok_or(RsrvMemAllocErr)
     }
 
     #[inline]
-    pub unsafe fn alloc_zeroed(&mut self, count: u32) -> Result<NonNull<u8>, RsrvMemAllocErr> {
+    pub unsafe fn alloc_zeroed(&self, count: u32) -> Result<NonNull<u8>, RsrvMemAllocErr> {
         NonNull::new(platform::alloc_zeroed(count)).ok_or(RsrvMemAllocErr)
     }
 
@@ -87,7 +87,7 @@
     /// Count of pages to allocate region
     ///
     #[inline]
-    pub unsafe fn dealloc(&mut self, addr: NonNull<u8>, count: u32) -> Result<(), RsrvMemAllocErr> {
+    pub unsafe fn dealloc(&self, addr: NonNull<u8>, count: u32) -> Result<(), RsrvMemAllocErr> {
         platform::dealloc(addr.as_ptr(), count)
     }
 
diff --git a/sgx_alloc/src/system.rs b/sgx_alloc/src/system.rs
index 47ac408..a29a723 100644
--- a/sgx_alloc/src/system.rs
+++ b/sgx_alloc/src/system.rs
@@ -23,7 +23,7 @@
 //! 2018-06-22 Add liballoc components here
 
 use core::alloc::{
-    AllocErr, AllocRef, GlobalAlloc, Layout,
+    AllocError, AllocRef, GlobalAlloc, Layout,
 };
 use core::intrinsics;
 use core::ptr::{self, NonNull};
@@ -43,7 +43,7 @@
 
 impl System {
     #[inline]
-    fn alloc_impl(&mut self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocErr> {
+    fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocError> {
         match layout.size() {
             0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
             // SAFETY: `layout` is non-zero in size,
@@ -53,7 +53,7 @@
                 } else {
                     GlobalAlloc::alloc(self, layout)
                 };
-                let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?;
+                let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
                 Ok(NonNull::slice_from_raw_parts(ptr, size))
             },
         }
@@ -62,12 +62,12 @@
     // Safety: Same as `AllocRef::grow`
     #[inline]
     unsafe fn grow_impl(
-        &mut self,
+        &self,
         ptr: NonNull<u8>,
         old_layout: Layout,
         new_layout: Layout,
         zeroed: bool,
-    ) -> Result<NonNull<[u8]>, AllocErr> {
+    ) -> Result<NonNull<[u8]>, AllocError> {
         debug_assert!(
             new_layout.size() >= old_layout.size(),
             "`new_layout.size()` must be greater than or equal to `old_layout.size()`"
@@ -85,7 +85,7 @@
                 intrinsics::assume(new_size >= old_layout.size());
 
                 let raw_ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), old_layout, new_size);
-                let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?;
+                let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
                 if zeroed {
                     raw_ptr.add(old_size).write_bytes(0, new_size - old_size);
                 }
@@ -100,7 +100,7 @@
             old_size => {
                 let new_ptr = self.alloc_impl(new_layout, zeroed)?;
                 ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), old_size);
-                self.dealloc(ptr, old_layout);
+                AllocRef::dealloc(&self, ptr, old_layout);
                 Ok(new_ptr)
             },
         }
@@ -109,17 +109,17 @@
 
 unsafe impl AllocRef for System {
     #[inline]
-    fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
+    fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
         self.alloc_impl(layout, false)
     }
 
     #[inline]
-    fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
+    fn alloc_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
          self.alloc_impl(layout, true)
     }
 
     #[inline]
-    unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
+    unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
         if layout.size() != 0 {
             GlobalAlloc::dealloc(self, ptr.as_ptr(), layout)
         }
@@ -127,47 +127,47 @@
 
     #[inline]
     unsafe fn grow(
-        &mut self,
+        &self,
         ptr: NonNull<u8>,
         old_layout: Layout,
         new_layout: Layout,
-    ) -> Result<NonNull<[u8]>, AllocErr> {
+    ) -> Result<NonNull<[u8]>, AllocError> {
          // SAFETY: all conditions must be upheld by the caller
          self.grow_impl(ptr, old_layout, new_layout, false)
     }
 
     #[inline]
     unsafe fn grow_zeroed(
-        &mut self,
+        &self,
         ptr: NonNull<u8>,
         old_layout: Layout,
         new_layout: Layout,
-    ) -> Result<NonNull<[u8]>, AllocErr> {
+    ) -> Result<NonNull<[u8]>, AllocError> {
         // SAFETY: all conditions must be upheld by the caller
         self.grow_impl(ptr, old_layout, new_layout, true)
     }
 
     #[inline]
     unsafe fn shrink(
-        &mut self,
+        &self,
         ptr: NonNull<u8>,
         old_layout: Layout,
         new_layout: Layout,
-    ) -> Result<NonNull<[u8]>, AllocErr> {
+    ) -> Result<NonNull<[u8]>, AllocError> {
          match new_layout.size() {
              // SAFETY: conditions must be upheld by the caller
              0 => {
-                 self.dealloc(ptr, old_layout);
-                 Ok(NonNull::slice_from_raw_parts(new_layout.dangling(), 0))
+                AllocRef::dealloc(&self, ptr, old_layout);
+                Ok(NonNull::slice_from_raw_parts(new_layout.dangling(), 0))
              },
              // SAFETY: `new_size` is non-zero. Other conditions must be upheld by the caller
              new_size if old_layout.align() == new_layout.align() => {
-                 // `realloc` probably checks for `new_size <= old_layout.size()` or something similar.
-                 intrinsics::assume(new_size <= old_layout.size());
+                // `realloc` probably checks for `new_size <= old_layout.size()` or something similar.
+                intrinsics::assume(new_size <= old_layout.size());
 
-                 let raw_ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), old_layout, new_size);
-                 let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?;
-                 Ok(NonNull::slice_from_raw_parts(ptr, new_size))
+                let raw_ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), old_layout, new_size);
+                let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
+                Ok(NonNull::slice_from_raw_parts(ptr, new_size))
              },
 
              // SAFETY: because `new_size` must be smaller than or equal to `old_layout.size()`,
@@ -176,10 +176,10 @@
              // `new_ptr`. Thus, the call to `copy_nonoverlapping` is safe. The safety contract
              // for `dealloc` must be upheld by the caller.
              new_size => {
-                 let new_ptr = self.alloc(new_layout)?;
-                 ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), new_size);
-                 self.dealloc(ptr, old_layout);
-                 Ok(new_ptr)
+                let new_ptr = AllocRef::alloc(&self, new_layout)?;
+                ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), new_size);
+                AllocRef::dealloc(&self, ptr, old_layout);
+                Ok(new_ptr)
              },
          }
     }
@@ -233,7 +233,7 @@
             if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
                 libc::calloc(layout.size(), 1) as *mut u8
             } else {
-                let ptr = self.alloc(layout);
+                let ptr = GlobalAlloc::alloc(self, layout);
                 if !ptr.is_null() {
                     ptr::write_bytes(ptr, 0, layout.size());
                 }
diff --git a/sgx_trts/src/oom.rs b/sgx_trts/src/oom.rs
index d14fd19..ad6530a 100644
--- a/sgx_trts/src/oom.rs
+++ b/sgx_trts/src/oom.rs
@@ -16,7 +16,7 @@
 // under the License..
 
 use crate::trts;
-use core::alloc::AllocErr;
+use core::alloc::AllocError;
 use core::mem;
 use core::ptr;
 use core::sync::atomic::{AtomicPtr, Ordering};
@@ -24,13 +24,13 @@
 static SGX_OOM_HANDLER: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut());
 
 #[allow(clippy::needless_pass_by_value)]
-fn default_oom_handler(_err: AllocErr) -> ! {
+fn default_oom_handler(_err: AllocError) -> ! {
     trts::rsgx_abort()
 }
 
-pub fn rsgx_oom(err: AllocErr) -> ! {
+pub fn rsgx_oom(err: AllocError) -> ! {
     let hook = SGX_OOM_HANDLER.load(Ordering::SeqCst);
-    let handler: fn(AllocErr) -> ! = if hook.is_null() {
+    let handler: fn(AllocError) -> ! = if hook.is_null() {
         default_oom_handler
     } else {
         unsafe { mem::transmute(hook) }
@@ -42,13 +42,13 @@
 ///
 /// To avoid recursive OOM failures, it is critical that the OOM handler does
 /// not allocate any memory itself.
-pub fn set_oom_handler(handler: fn(AllocErr) -> !) {
+pub fn set_oom_handler(handler: fn(AllocError) -> !) {
     SGX_OOM_HANDLER.store(handler as *mut (), Ordering::SeqCst);
 }
 
 /// Unregisters the current custom handler, returning it.
 ///
-pub fn take_oom_handler() -> fn(AllocErr) -> ! {
+pub fn take_oom_handler() -> fn(AllocError) -> ! {
     let hook = SGX_OOM_HANDLER.swap(ptr::null_mut(), Ordering::SeqCst);
     if hook.is_null() {
         default_oom_handler
diff --git a/sgx_tstd/src/error.rs b/sgx_tstd/src/error.rs
index f01d8a0..7fd4d49 100644
--- a/sgx_tstd/src/error.rs
+++ b/sgx_tstd/src/error.rs
@@ -17,7 +17,7 @@
 
 #[cfg(feature = "backtrace")]
 use crate::backtrace::Backtrace;
-use core::alloc::{AllocErr, LayoutErr};
+use core::alloc::{AllocError, LayoutErr};
 use core::array;
 use core::any::TypeId;
 use core::cell;
@@ -203,7 +203,7 @@
     }
 }
 
-impl Error for AllocErr {
+impl Error for AllocError {
     fn description(&self) -> &str {
         "memory allocation failed"
     }
diff --git a/sgx_tstd/src/lib.rs b/sgx_tstd/src/lib.rs
index 08b52e0..74d1ca7 100644
--- a/sgx_tstd/src/lib.rs
+++ b/sgx_tstd/src/lib.rs
@@ -56,6 +56,7 @@
 #![feature(char_error_internals)]
 #![feature(concat_idents)]
 #![feature(const_fn)]
+#![feature(const_fn_fn_ptr_basics)]
 #![feature(core_intrinsics)]
 #![feature(custom_test_frameworks)]
 #![feature(dropck_eyepatch)]
diff --git a/sgx_tstd/src/sync/condvar.rs b/sgx_tstd/src/sync/condvar.rs
index 71929c8..48aefbe 100644
--- a/sgx_tstd/src/sync/condvar.rs
+++ b/sgx_tstd/src/sync/condvar.rs
@@ -32,7 +32,7 @@
 use sgx_trts::libc;
 use core::sync::atomic::{AtomicUsize, Ordering};
 use core::fmt;
-use core::alloc::AllocErr;
+use core::alloc::AllocError;
 use alloc_crate::boxed::Box;
 use crate::sync::{mutex, SgxThreadMutex, SgxMutexGuard};
 use crate::sys_common::poison::{self, LockResult, PoisonError};
@@ -426,7 +426,7 @@
             let ret = self.inner.broadcast();
             match ret {
                 Err(r) if r == libc::ENOMEM => {
-                    oom::rsgx_oom(AllocErr)
+                    oom::rsgx_oom(AllocError)
                 },
                 _ => {},
             }
@@ -483,4 +483,4 @@
         let result = unsafe { self.inner.destroy() };
         debug_assert_eq!(result, Ok(()), "Error when destroy an SgxCondvar: {}", result.unwrap_err());
     }
-}
\ No newline at end of file
+}
diff --git a/sgx_tstd/src/sys/cmath.rs b/sgx_tstd/src/sys/cmath.rs
index a21825c..c5df605 100644
--- a/sgx_tstd/src/sys/cmath.rs
+++ b/sgx_tstd/src/sys/cmath.rs
@@ -17,7 +17,7 @@
 
 use sgx_libc::{c_float, c_double};
 
-#[link_name = "sgx_tstdc"]
+#[link(name = "sgx_tstdc")]
 extern "C" {
     pub fn acos(n: c_double) -> c_double;
     pub fn acosf(n: c_float) -> c_float;
@@ -45,4 +45,4 @@
     pub fn tanf(n: c_float) -> c_float;
     pub fn tanh(n: c_double) -> c_double;
     pub fn tanhf(n: c_float) -> c_float;
-}
\ No newline at end of file
+}