3 use ptr::{self, NonNull};
7 use ops::{Deref, DerefMut, Index, IndexMut, CoerceUnsized};
10 use fortanix_sgx_abi::*;
11 use super::super::mem::is_user_range;
13 /// A type that can be safely read from or written to userspace.
15 /// Non-exhaustive list of specific requirements for reading and writing:
16 /// * **Type is `Copy`** (and therefore also not `Drop`). Copies will be
17 /// created when copying from/to userspace. Destructors will not be called.
18 /// * **No references or Rust-style owned pointers** (`Vec`, `Arc`, etc.). When
19 /// reading from userspace, references into enclave memory must not be
20 /// created. Also, only enclave memory is considered managed by the Rust
21 /// compiler's static analysis. When reading from userspace, there can be no
22 /// guarantee that the value correctly adheres to the expectations of the
23 /// type. When writing to userspace, memory addresses of data in enclave
24 /// memory must not be leaked for confidentiality reasons. `User` and
25 /// `UserRef` are also not allowed for the same reasons.
26 /// * **No fat pointers.** When reading from userspace, the size or vtable
27 /// pointer could be automatically interpreted and used by the code. When
28 /// writing to userspace, memory addresses of data in enclave memory (such
29 /// as vtable pointers) must not be leaked for confidentiality reasons.
31 /// Non-exhaustive list of specific requirements for reading from userspace:
32 /// * **Any bit pattern is valid** for this type (no `enum`s). There can be no
33 /// guarantee that the value correctly adheres to the expectations of the
34 /// type, so any value must be valid for this type.
36 /// Non-exhaustive list of specific requirements for writing to userspace:
37 /// * **No pointers to enclave memory.** Memory addresses of data in enclave
38 /// memory must not be leaked for confidentiality reasons.
39 /// * **No internal padding.** Padding might contain previously-initialized
40 /// secret data stored at that memory location and must not be leaked for
41 /// confidentiality reasons.
42 #[unstable(feature = "sgx_platform", issue = "56975")]
43 pub unsafe trait UserSafeSized: Copy + Sized {}
45 #[unstable(feature = "sgx_platform", issue = "56975")]
46 unsafe impl UserSafeSized for u8 {}
47 #[unstable(feature = "sgx_platform", issue = "56975")]
48 unsafe impl<T> UserSafeSized for FifoDescriptor<T> {}
49 #[unstable(feature = "sgx_platform", issue = "56975")]
50 unsafe impl UserSafeSized for ByteBuffer {}
51 #[unstable(feature = "sgx_platform", issue = "56975")]
52 unsafe impl UserSafeSized for Usercall {}
53 #[unstable(feature = "sgx_platform", issue = "56975")]
54 unsafe impl UserSafeSized for Return {}
55 #[unstable(feature = "sgx_platform", issue = "56975")]
56 unsafe impl<T: UserSafeSized> UserSafeSized for [T; 2] {}
58 /// A type that can be represented in memory as one or more `UserSafeSized`s.
59 #[unstable(feature = "sgx_platform", issue = "56975")]
60 pub unsafe trait UserSafe {
61 /// Equivalent to `mem::align_of::<Self>`.
62 fn align_of() -> usize;
64 /// Construct a pointer to `Self` given a memory range in user space.
66 /// NB. This takes a size, not a length!
69 /// The caller must ensure the memory range is in user memory, is the
70 /// correct size and is correctly aligned and points to the right type.
71 unsafe fn from_raw_sized_unchecked(ptr: *mut u8, size: usize) -> *mut Self;
73 /// Construct a pointer to `Self` given a memory range.
75 /// NB. This takes a size, not a length!
78 /// The caller must ensure the memory range points to the correct type.
81 /// This function panics if:
83 /// * The pointer is not aligned
84 /// * The pointer is null
85 /// * The pointed-to range is not in user memory
86 unsafe fn from_raw_sized(ptr: *mut u8, size: usize) -> NonNull<Self> {
87 let ret = Self::from_raw_sized_unchecked(ptr, size);
89 NonNull::new_unchecked(ret as _)
92 /// Check if a pointer may point to Self in user memory.
95 /// The caller must ensure the memory range points to the correct type and
96 /// length (if this is a slice).
99 /// This function panics if:
101 /// * The pointer is not aligned
102 /// * The pointer is null
103 /// * The pointed-to range is not in user memory
104 unsafe fn check_ptr(ptr: *const Self) {
105 let is_aligned = |p| -> bool {
106 0 == (p as usize) & (Self::align_of() - 1)
109 assert!(is_aligned(ptr as *const u8));
110 assert!(is_user_range(ptr as _, mem::size_of_val(&*ptr)));
111 assert!(!ptr.is_null());
115 #[unstable(feature = "sgx_platform", issue = "56975")]
116 unsafe impl<T: UserSafeSized> UserSafe for T {
117 fn align_of() -> usize {
121 unsafe fn from_raw_sized_unchecked(ptr: *mut u8, size: usize) -> *mut Self {
122 assert_eq!(size, mem::size_of::<T>());
127 #[unstable(feature = "sgx_platform", issue = "56975")]
128 unsafe impl<T: UserSafeSized> UserSafe for [T] {
129 fn align_of() -> usize {
133 unsafe fn from_raw_sized_unchecked(ptr: *mut u8, size: usize) -> *mut Self {
134 let elem_size = mem::size_of::<T>();
135 assert_eq!(size % elem_size, 0);
136 let len = size / elem_size;
137 slice::from_raw_parts_mut(ptr as _, len)
141 /// A reference to some type in userspace memory. `&UserRef<T>` is equivalent
142 /// to `&T` in enclave memory. Access to the memory is only allowed by copying
143 /// to avoid TOCTTOU issues. After copying, code should make sure to completely
144 /// check the value before use.
146 /// It is also possible to obtain a mutable reference `&mut UserRef<T>`. Unlike
147 /// regular mutable references, these are not exclusive. Userspace may always
148 /// write to the backing memory at any time, so it can't be assumed that there
149 /// the pointed-to memory is uniquely borrowed. The two different refence types
150 /// are used solely to indicate intent: a mutable reference is for writing to
151 /// user memory, an immutable reference for reading from user memory.
152 #[unstable(feature = "sgx_platform", issue = "56975")]
153 pub struct UserRef<T: ?Sized>(UnsafeCell<T>);
154 /// An owned type in userspace memory. `User<T>` is equivalent to `Box<T>` in
155 /// enclave memory. Access to the memory is only allowed by copying to avoid
156 /// TOCTTOU issues. The user memory will be freed when the value is dropped.
157 /// After copying, code should make sure to completely check the value before
159 #[unstable(feature = "sgx_platform", issue = "56975")]
160 pub struct User<T: UserSafe + ?Sized>(NonNull<UserRef<T>>);
162 trait NewUserRef<T: ?Sized> {
163 unsafe fn new_userref(v: T) -> Self;
166 impl<T: ?Sized> NewUserRef<*mut T> for NonNull<UserRef<T>> {
167 unsafe fn new_userref(v: *mut T) -> Self {
168 NonNull::new_unchecked(v as _)
172 impl<T: ?Sized> NewUserRef<NonNull<T>> for NonNull<UserRef<T>> {
173 unsafe fn new_userref(v: NonNull<T>) -> Self {
174 NonNull::new_userref(v.as_ptr())
178 #[unstable(feature = "sgx_platform", issue = "56975")]
179 impl<T: ?Sized> User<T> where T: UserSafe {
180 // This function returns memory that is practically uninitialized, but is
181 // not considered "unspecified" or "undefined" for purposes of an
182 // optimizing compiler. This is achieved by returning a pointer from
183 // from outside as obtained by `super::alloc`.
184 fn new_uninit_bytes(size: usize) -> Self {
186 let ptr = super::alloc(size, T::align_of()).expect("User memory allocation failed");
187 User(NonNull::new_userref(T::from_raw_sized(ptr as _, size)))
191 /// Copy `val` into freshly allocated space in user memory.
192 pub fn new_from_enclave(val: &T) -> Self {
194 let ret = Self::new_uninit_bytes(mem::size_of_val(val));
196 val as *const T as *const u8,
197 ret.0.as_ptr() as *mut u8,
198 mem::size_of_val(val)
204 /// Create an owned `User<T>` from a raw pointer.
207 /// The caller must ensure `ptr` points to `T`, is freeable with the `free`
208 /// usercall and the alignment of `T`, and is uniquely owned.
211 /// This function panics if:
213 /// * The pointer is not aligned
214 /// * The pointer is null
215 /// * The pointed-to range is not in user memory
216 pub unsafe fn from_raw(ptr: *mut T) -> Self {
218 User(NonNull::new_userref(ptr))
221 /// Convert this value into a raw pointer. The value will no longer be
222 /// automatically freed.
223 pub fn into_raw(self) -> *mut T {
230 #[unstable(feature = "sgx_platform", issue = "56975")]
231 impl<T> User<T> where T: UserSafe {
232 /// Allocate space for `T` in user memory.
233 pub fn uninitialized() -> Self {
234 Self::new_uninit_bytes(mem::size_of::<T>())
238 #[unstable(feature = "sgx_platform", issue = "56975")]
239 impl<T> User<[T]> where [T]: UserSafe {
240 /// Allocate space for a `[T]` of `n` elements in user memory.
241 pub fn uninitialized(n: usize) -> Self {
242 Self::new_uninit_bytes(n * mem::size_of::<T>())
245 /// Create an owned `User<[T]>` from a raw thin pointer and a slice length.
248 /// The caller must ensure `ptr` points to `len` elements of `T`, is
249 /// freeable with the `free` usercall and the alignment of `T`, and is
253 /// This function panics if:
255 /// * The pointer is not aligned
256 /// * The pointer is null
257 /// * The pointed-to range is not in user memory
258 pub unsafe fn from_raw_parts(ptr: *mut T, len: usize) -> Self {
259 User(NonNull::new_userref(<[T]>::from_raw_sized(ptr as _, len * mem::size_of::<T>())))
263 #[unstable(feature = "sgx_platform", issue = "56975")]
264 impl<T: ?Sized> UserRef<T> where T: UserSafe {
265 /// Create a `&UserRef<[T]>` from a raw pointer.
268 /// The caller must ensure `ptr` points to `T`.
271 /// This function panics if:
273 /// * The pointer is not aligned
274 /// * The pointer is null
275 /// * The pointed-to range is not in user memory
276 pub unsafe fn from_ptr<'a>(ptr: *const T) -> &'a Self {
278 &*(ptr as *const Self)
281 /// Create a `&mut UserRef<[T]>` from a raw pointer. See the struct
282 /// documentation for the nuances regarding a `&mut UserRef<T>`.
285 /// The caller must ensure `ptr` points to `T`.
288 /// This function panics if:
290 /// * The pointer is not aligned
291 /// * The pointer is null
292 /// * The pointed-to range is not in user memory
293 pub unsafe fn from_mut_ptr<'a>(ptr: *mut T) -> &'a mut Self {
295 &mut*(ptr as *mut Self)
298 /// Copy `val` into user memory.
301 /// This function panics if the destination doesn't have the same size as
302 /// the source. This can happen for dynamically-sized types such as slices.
303 pub fn copy_from_enclave(&mut self, val: &T) {
305 assert_eq!(mem::size_of_val(val), mem::size_of_val( &*self.0.get() ));
307 val as *const T as *const u8,
308 self.0.get() as *mut T as *mut u8,
309 mem::size_of_val(val)
314 /// Copy the value from user memory and place it into `dest`.
317 /// This function panics if the destination doesn't have the same size as
318 /// the source. This can happen for dynamically-sized types such as slices.
319 pub fn copy_to_enclave(&self, dest: &mut T) {
321 assert_eq!(mem::size_of_val(dest), mem::size_of_val( &*self.0.get() ));
323 self.0.get() as *const T as *const u8,
324 dest as *mut T as *mut u8,
325 mem::size_of_val(dest)
330 /// Obtain a raw pointer from this reference.
331 pub fn as_raw_ptr(&self) -> *const T {
332 self as *const _ as _
335 /// Obtain a raw pointer from this reference.
336 pub fn as_raw_mut_ptr(&mut self) -> *mut T {
341 #[unstable(feature = "sgx_platform", issue = "56975")]
342 impl<T> UserRef<T> where T: UserSafe {
343 /// Copy the value from user memory into enclave memory.
344 pub fn to_enclave(&self) -> T {
345 unsafe { ptr::read(self.0.get()) }
349 #[unstable(feature = "sgx_platform", issue = "56975")]
350 impl<T> UserRef<[T]> where [T]: UserSafe {
351 /// Create a `&UserRef<[T]>` from a raw thin pointer and a slice length.
354 /// The caller must ensure `ptr` points to `n` elements of `T`.
357 /// This function panics if:
359 /// * The pointer is not aligned
360 /// * The pointer is null
361 /// * The pointed-to range is not in user memory
362 pub unsafe fn from_raw_parts<'a>(ptr: *const T, len: usize) -> &'a Self {
363 &*(<[T]>::from_raw_sized(ptr as _, len * mem::size_of::<T>()).as_ptr() as *const Self)
366 /// Create a `&mut UserRef<[T]>` from a raw thin pointer and a slice length.
367 /// See the struct documentation for the nuances regarding a
368 /// `&mut UserRef<T>`.
371 /// The caller must ensure `ptr` points to `n` elements of `T`.
374 /// This function panics if:
376 /// * The pointer is not aligned
377 /// * The pointer is null
378 /// * The pointed-to range is not in user memory
379 pub unsafe fn from_raw_parts_mut<'a>(ptr: *mut T, len: usize) -> &'a mut Self {
380 &mut*(<[T]>::from_raw_sized(ptr as _, len * mem::size_of::<T>()).as_ptr() as *mut Self)
383 /// Obtain a raw pointer to the first element of this user slice.
384 pub fn as_ptr(&self) -> *const T {
388 /// Obtain a raw pointer to the first element of this user slice.
389 pub fn as_mut_ptr(&mut self) -> *mut T {
393 /// Obtain the number of elements in this user slice.
394 pub fn len(&self) -> usize {
395 unsafe { (*self.0.get()).len() }
398 /// Copy the value from user memory and place it into `dest`. Afterwards,
399 /// `dest` will contain exactly `self.len()` elements.
402 /// This function panics if the destination doesn't have the same size as
403 /// the source. This can happen for dynamically-sized types such as slices.
404 pub fn copy_to_enclave_vec(&self, dest: &mut Vec<T>) {
406 if let Some(missing) = self.len().checked_sub(dest.capacity()) {
407 dest.reserve(missing)
409 dest.set_len(self.len());
410 self.copy_to_enclave(&mut dest[..]);
414 /// Copy the value from user memory into a vector in enclave memory.
415 pub fn to_enclave(&self) -> Vec<T> {
416 let mut ret = Vec::with_capacity(self.len());
417 self.copy_to_enclave_vec(&mut ret);
421 /// Returns an iterator over the slice.
422 pub fn iter(&self) -> Iter<T>
423 where T: UserSafe // FIXME: should be implied by [T]: UserSafe?
426 Iter((&*self.as_raw_ptr()).iter())
430 /// Returns an iterator that allows modifying each value.
431 pub fn iter_mut(&mut self) -> IterMut<T>
432 where T: UserSafe // FIXME: should be implied by [T]: UserSafe?
435 IterMut((&mut*self.as_raw_mut_ptr()).iter_mut())
440 /// Immutable user slice iterator
442 /// This struct is created by the `iter` method on `UserRef<[T]>`.
443 #[unstable(feature = "sgx_platform", issue = "56975")]
444 pub struct Iter<'a, T: 'a + UserSafe>(slice::Iter<'a, T>);
446 #[unstable(feature = "sgx_platform", issue = "56975")]
447 impl<'a, T: UserSafe> Iterator for Iter<'a, T> {
448 type Item = &'a UserRef<T>;
451 fn next(&mut self) -> Option<Self::Item> {
453 self.0.next().map(|e| UserRef::from_ptr(e))
458 /// Mutable user slice iterator
460 /// This struct is created by the `iter_mut` method on `UserRef<[T]>`.
461 #[unstable(feature = "sgx_platform", issue = "56975")]
462 pub struct IterMut<'a, T: 'a + UserSafe>(slice::IterMut<'a, T>);
464 #[unstable(feature = "sgx_platform", issue = "56975")]
465 impl<'a, T: UserSafe> Iterator for IterMut<'a, T> {
466 type Item = &'a mut UserRef<T>;
469 fn next(&mut self) -> Option<Self::Item> {
471 self.0.next().map(|e| UserRef::from_mut_ptr(e))
476 #[unstable(feature = "sgx_platform", issue = "56975")]
477 impl<T: ?Sized> Deref for User<T> where T: UserSafe {
478 type Target = UserRef<T>;
480 fn deref(&self) -> &Self::Target {
481 unsafe { &*self.0.as_ptr() }
485 #[unstable(feature = "sgx_platform", issue = "56975")]
486 impl<T: ?Sized> DerefMut for User<T> where T: UserSafe {
487 fn deref_mut(&mut self) -> &mut Self::Target {
488 unsafe { &mut*self.0.as_ptr() }
492 #[unstable(feature = "sgx_platform", issue = "56975")]
493 impl<T: ?Sized> Drop for User<T> where T: UserSafe {
496 let ptr = (*self.0.as_ptr()).0.get();
497 super::free(ptr as _, mem::size_of_val(&mut*ptr), T::align_of());
502 #[unstable(feature = "sgx_platform", issue = "56975")]
503 impl<T: CoerceUnsized<U>, U> CoerceUnsized<UserRef<U>> for UserRef<T> {}
505 #[unstable(feature = "sgx_platform", issue = "56975")]
506 impl<T, I: SliceIndex<[T]>> Index<I> for UserRef<[T]> where [T]: UserSafe, I::Output: UserSafe {
507 type Output = UserRef<I::Output>;
510 fn index(&self, index: I) -> &UserRef<I::Output> {
512 UserRef::from_ptr(index.index(&*self.as_raw_ptr()))
517 #[unstable(feature = "sgx_platform", issue = "56975")]
518 impl<T, I: SliceIndex<[T]>> IndexMut<I> for UserRef<[T]> where [T]: UserSafe, I::Output: UserSafe {
520 fn index_mut(&mut self, index: I) -> &mut UserRef<I::Output> {
522 UserRef::from_mut_ptr(index.index_mut(&mut*self.as_raw_mut_ptr()))
527 #[unstable(feature = "sgx_platform", issue = "56975")]
528 impl UserRef<super::raw::ByteBuffer> {
529 /// Copy the user memory range pointed to by the user `ByteBuffer` to
533 /// This function panics if:
535 /// * The pointer in the user `ByteBuffer` is null
536 /// * The pointed-to range in the user `ByteBuffer` is not in user memory
537 pub fn copy_user_buffer(&self) -> Vec<u8> {
539 let buf = self.to_enclave();
540 User::from_raw_parts(buf.data as _, buf.len).to_enclave()