@@ -89,9 +89,12 @@ pub unsafe trait UserSafe {
8989 /// * the pointed-to range is not in user memory.
9090 unsafe fn from_raw_sized ( ptr : * mut u8 , size : usize ) -> NonNull < Self > {
9191 assert ! ( ptr. wrapping_add( size) >= ptr) ;
92- let ret = Self :: from_raw_sized_unchecked ( ptr, size) ;
93- Self :: check_ptr ( ret) ;
94- NonNull :: new_unchecked ( ret as _ )
92+ // SAFETY: The caller has guaranteed the pointer is valid
93+ let ret = unsafe { Self :: from_raw_sized_unchecked ( ptr, size) } ;
94+ unsafe {
95+ Self :: check_ptr ( ret) ;
96+ NonNull :: new_unchecked ( ret as _ )
97+ }
9598 }
9699
97100 /// Checks if a pointer may point to `Self` in user memory.
@@ -112,7 +115,7 @@ pub unsafe trait UserSafe {
112115 let is_aligned = |p| -> bool { 0 == ( p as usize ) & ( Self :: align_of ( ) - 1 ) } ;
113116
114117 assert ! ( is_aligned( ptr as * const u8 ) ) ;
115- assert ! ( is_user_range( ptr as _, mem:: size_of_val( & * ptr) ) ) ;
118+ assert ! ( is_user_range( ptr as _, mem:: size_of_val( unsafe { & * ptr } ) ) ) ;
116119 assert ! ( !ptr. is_null( ) ) ;
117120 }
118121}
@@ -135,11 +138,23 @@ unsafe impl<T: UserSafeSized> UserSafe for [T] {
135138 mem:: align_of :: < T > ( )
136139 }
137140
141+ /// # Safety
142+ /// Behavior is undefined if any of these conditions are violated:
143+ /// * `ptr` must be [valid] for writes of `size` many bytes, and it must be
144+ /// properly aligned.
145+ ///
146+ /// [valid]: core::ptr#safety
147+ /// # Panics
148+ ///
149+ /// This function panics if:
150+ ///
151+ /// * the element size is not a factor of the size
138152 unsafe fn from_raw_sized_unchecked ( ptr : * mut u8 , size : usize ) -> * mut Self {
139153 let elem_size = mem:: size_of :: < T > ( ) ;
140154 assert_eq ! ( size % elem_size, 0 ) ;
141155 let len = size / elem_size;
142- slice:: from_raw_parts_mut ( ptr as _ , len)
156+ // SAFETY: The caller must uphold the safety contract for `from_raw_sized_unchecked`
157+ unsafe { slice:: from_raw_parts_mut ( ptr as _ , len) }
143158 }
144159}
145160
@@ -170,13 +185,15 @@ trait NewUserRef<T: ?Sized> {
170185
171186impl < T : ?Sized > NewUserRef < * mut T > for NonNull < UserRef < T > > {
172187 unsafe fn new_userref ( v : * mut T ) -> Self {
173- NonNull :: new_unchecked ( v as _ )
188+ // SAFETY: The caller has guaranteed the pointer is valid
189+ unsafe { NonNull :: new_unchecked ( v as _ ) }
174190 }
175191}
176192
177193impl < T : ?Sized > NewUserRef < NonNull < T > > for NonNull < UserRef < T > > {
178194 unsafe fn new_userref ( v : NonNull < T > ) -> Self {
179- NonNull :: new_userref ( v. as_ptr ( ) )
195+ // SAFETY: The caller has guaranteed the pointer is valid
196+ unsafe { NonNull :: new_userref ( v. as_ptr ( ) ) }
180197 }
181198}
182199
@@ -231,8 +248,9 @@ where
231248 /// * The pointer is null
232249 /// * The pointed-to range is not in user memory
233250 pub unsafe fn from_raw ( ptr : * mut T ) -> Self {
234- T :: check_ptr ( ptr) ;
235- User ( NonNull :: new_userref ( ptr) )
251+ // SAFETY: the caller must uphold the safety contract for `from_raw`.
252+ unsafe { T :: check_ptr ( ptr) } ;
253+ User ( unsafe { NonNull :: new_userref ( ptr) } )
236254 }
237255
238256 /// Converts this value into a raw pointer. The value will no longer be
@@ -280,7 +298,9 @@ where
280298 /// * The pointed-to range does not fit in the address space
281299 /// * The pointed-to range is not in user memory
282300 pub unsafe fn from_raw_parts ( ptr : * mut T , len : usize ) -> Self {
283- User ( NonNull :: new_userref ( <[ T ] >:: from_raw_sized ( ptr as _ , len * mem:: size_of :: < T > ( ) ) ) )
301+ User ( unsafe {
302+ NonNull :: new_userref ( <[ T ] >:: from_raw_sized ( ptr as _ , len * mem:: size_of :: < T > ( ) ) )
303+ } )
284304 }
285305}
286306
@@ -301,8 +321,9 @@ where
301321 /// * The pointer is null
302322 /// * The pointed-to range is not in user memory
303323 pub unsafe fn from_ptr < ' a > ( ptr : * const T ) -> & ' a Self {
304- T :: check_ptr ( ptr) ;
305- & * ( ptr as * const Self )
324+ // SAFETY: The caller must uphold the safety contract for `from_ptr`.
325+ unsafe { T :: check_ptr ( ptr) } ;
326+ unsafe { & * ( ptr as * const Self ) }
306327 }
307328
308329 /// Creates a `&mut UserRef<[T]>` from a raw pointer. See the struct
@@ -318,8 +339,9 @@ where
318339 /// * The pointer is null
319340 /// * The pointed-to range is not in user memory
320341 pub unsafe fn from_mut_ptr < ' a > ( ptr : * mut T ) -> & ' a mut Self {
321- T :: check_ptr ( ptr) ;
322- & mut * ( ptr as * mut Self )
342+ // SAFETY: The caller must uphold the safety contract for `from_mut_ptr`.
343+ unsafe { T :: check_ptr ( ptr) } ;
344+ unsafe { & mut * ( ptr as * mut Self ) }
323345 }
324346
325347 /// Copies `val` into user memory.
@@ -394,7 +416,10 @@ where
394416 /// * The pointed-to range does not fit in the address space
395417 /// * The pointed-to range is not in user memory
396418 pub unsafe fn from_raw_parts < ' a > ( ptr : * const T , len : usize ) -> & ' a Self {
397- & * ( <[ T ] >:: from_raw_sized ( ptr as _ , len * mem:: size_of :: < T > ( ) ) . as_ptr ( ) as * const Self )
419+ // SAFETY: The caller must uphold the safety contract for `from_raw_parts`.
420+ unsafe {
421+ & * ( <[ T ] >:: from_raw_sized ( ptr as _ , len * mem:: size_of :: < T > ( ) ) . as_ptr ( ) as * const Self )
422+ }
398423 }
399424
400425 /// Creates a `&mut UserRef<[T]>` from a raw thin pointer and a slice length.
@@ -412,7 +437,10 @@ where
412437 /// * The pointed-to range does not fit in the address space
413438 /// * The pointed-to range is not in user memory
414439 pub unsafe fn from_raw_parts_mut < ' a > ( ptr : * mut T , len : usize ) -> & ' a mut Self {
415- & mut * ( <[ T ] >:: from_raw_sized ( ptr as _ , len * mem:: size_of :: < T > ( ) ) . as_ptr ( ) as * mut Self )
440+ // SAFETY: The caller must uphold the safety contract for `from_raw_parts_mut`.
441+ unsafe {
442+ & mut * ( <[ T ] >:: from_raw_sized ( ptr as _ , len * mem:: size_of :: < T > ( ) ) . as_ptr ( ) as * mut Self )
443+ }
416444 }
417445
418446 /// Obtain a raw pointer to the first element of this user slice.
@@ -437,13 +465,12 @@ where
437465 /// This function panics if the destination doesn't have the same size as
438466 /// the source. This can happen for dynamically-sized types such as slices.
439467 pub fn copy_to_enclave_vec ( & self , dest : & mut Vec < T > ) {
440- unsafe {
441- if let Some ( missing) = self . len ( ) . checked_sub ( dest. capacity ( ) ) {
442- dest. reserve ( missing)
443- }
444- dest. set_len ( self . len ( ) ) ;
445- self . copy_to_enclave ( & mut dest[ ..] ) ;
468+ if let Some ( missing) = self . len ( ) . checked_sub ( dest. capacity ( ) ) {
469+ dest. reserve ( missing)
446470 }
471+ // SAFETY: We reserve enough space above.
472+ unsafe { dest. set_len ( self . len ( ) ) } ;
473+ self . copy_to_enclave ( & mut dest[ ..] ) ;
447474 }
448475
449476 /// Copies the value from user memory into a vector in enclave memory.
0 commit comments