Skip to content

Commit b526668

Browse files
Jethro Beekmanthaliaarchi
andcommitted
Copy from userspace to MaybeUninit
Co-authored-by: Thalia Archibald <thalia@archibald.dev>
1 parent 2c6a12e commit b526668

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

  • library/std/src/sys/pal/sgx/abi/usercalls

‎library/std/src/sys/pal/sgx/abi/usercalls/alloc.rs‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::super::mem::{is_enclave_range, is_user_range};
66
usecrate::arch::asm;
77
usecrate::cell::UnsafeCell;
88
usecrate::convert::TryInto;
9-
usecrate::mem::{self,ManuallyDrop};
9+
usecrate::mem::{self,ManuallyDrop,MaybeUninit};
1010
usecrate::ops::{CoerceUnsized,Deref,DerefMut,Index,IndexMut};
1111
usecrate::pin::PinCoerceUnsized;
1212
usecrate::ptr::{self,NonNull};
@@ -209,6 +209,45 @@ impl<T: ?Sized> NewUserRef<NonNull<T>> for NonNull<UserRef<T>> {
209209
}
210210
}
211211

212+
/// A type which can a destination for safely copying from userspace.
213+
///
214+
/// # Safety
215+
///
216+
/// Requires that `T` and `Self` have identical layouts.
217+
#[unstable(feature = "sgx_platform", issue = "56975")]
218+
pubunsafetraitUserSafeCopyDestination<T: ?Sized>{
219+
/// Returns a pointer for writing to the value.
220+
fnas_mut_ptr(&mutself) -> *mutT;
221+
}
222+
223+
#[unstable(feature = "sgx_platform", issue = "56975")]
224+
unsafeimpl<T>UserSafeCopyDestination<T>forT{
225+
fnas_mut_ptr(&mutself) -> *mutT{
226+
selfas_
227+
}
228+
}
229+
230+
#[unstable(feature = "sgx_platform", issue = "56975")]
231+
unsafeimpl<T>UserSafeCopyDestination<[T]>for[T]{
232+
fnas_mut_ptr(&mutself) -> *mut[T]{
233+
selfas_
234+
}
235+
}
236+
237+
#[unstable(feature = "sgx_platform", issue = "56975")]
238+
unsafeimpl<T>UserSafeCopyDestination<T>forMaybeUninit<T>{
239+
fnas_mut_ptr(&mutself) -> *mutT{
240+
selfas*mutSelfas_
241+
}
242+
}
243+
244+
#[unstable(feature = "sgx_platform", issue = "56975")]
245+
unsafeimpl<T>UserSafeCopyDestination<[T]>for[MaybeUninit<T>]{
246+
fnas_mut_ptr(&mutself) -> *mut[T]{
247+
selfas*mutSelfas_
248+
}
249+
}
250+
212251
#[unstable(feature = "sgx_platform", issue = "56975")]
213252
impl<T: ?Sized>User<T>
214253
where
@@ -544,12 +583,12 @@ where
544583
/// # Panics
545584
/// This function panics if the destination doesn't have the same size as
546585
/// the source. This can happen for dynamically-sized types such as slices.
547-
pubfncopy_to_enclave(&self,dest:&mutT){
586+
pubfncopy_to_enclave<U: ?Sized + UserSafeCopyDestination<T>>(&self,dest:&mutU){
548587
unsafe{
549588
assert_eq!(size_of_val(dest), size_of_val(&*self.0.get()));
550589
copy_from_userspace(
551590
self.0.get()as*constTas*constu8,
552-
destas*mutTas*mutu8,
591+
dest.as_mut_ptr()as*mutu8,
553592
size_of_val(dest),
554593
);
555594
}

0 commit comments

Comments
 (0)