@@ -89,9 +89,12 @@ pub unsafe trait UserSafe {
89
89
/// * the pointed-to range is not in user memory.
90
90
unsafe fn from_raw_sized ( ptr : * mut u8 , size : usize ) -> NonNull < Self > {
91
91
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
+ }
95
98
}
96
99
97
100
/// Checks if a pointer may point to `Self` in user memory.
@@ -112,7 +115,7 @@ pub unsafe trait UserSafe {
112
115
let is_aligned = |p| -> bool { 0 == ( p as usize ) & ( Self :: align_of ( ) - 1 ) } ;
113
116
114
117
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 } ) ) ) ;
116
119
assert ! ( !ptr. is_null( ) ) ;
117
120
}
118
121
}
@@ -135,11 +138,23 @@ unsafe impl<T: UserSafeSized> UserSafe for [T] {
135
138
mem:: align_of :: < T > ( )
136
139
}
137
140
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
138
152
unsafe fn from_raw_sized_unchecked ( ptr : * mut u8 , size : usize ) -> * mut Self {
139
153
let elem_size = mem:: size_of :: < T > ( ) ;
140
154
assert_eq ! ( size % elem_size, 0 ) ;
141
155
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) }
143
158
}
144
159
}
145
160
@@ -170,13 +185,15 @@ trait NewUserRef<T: ?Sized> {
170
185
171
186
impl < T : ?Sized > NewUserRef < * mut T > for NonNull < UserRef < T > > {
172
187
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 _ ) }
174
190
}
175
191
}
176
192
177
193
impl < T : ?Sized > NewUserRef < NonNull < T > > for NonNull < UserRef < T > > {
178
194
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 ( ) ) }
180
197
}
181
198
}
182
199
@@ -231,8 +248,9 @@ where
231
248
/// * The pointer is null
232
249
/// * The pointed-to range is not in user memory
233
250
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) } )
236
254
}
237
255
238
256
/// Converts this value into a raw pointer. The value will no longer be
@@ -280,7 +298,9 @@ where
280
298
/// * The pointed-to range does not fit in the address space
281
299
/// * The pointed-to range is not in user memory
282
300
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
+ } )
284
304
}
285
305
}
286
306
@@ -301,8 +321,9 @@ where
301
321
/// * The pointer is null
302
322
/// * The pointed-to range is not in user memory
303
323
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 ) }
306
327
}
307
328
308
329
/// Creates a `&mut UserRef<[T]>` from a raw pointer. See the struct
@@ -318,8 +339,9 @@ where
318
339
/// * The pointer is null
319
340
/// * The pointed-to range is not in user memory
320
341
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 ) }
323
345
}
324
346
325
347
/// Copies `val` into user memory.
@@ -394,7 +416,10 @@ where
394
416
/// * The pointed-to range does not fit in the address space
395
417
/// * The pointed-to range is not in user memory
396
418
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
+ }
398
423
}
399
424
400
425
/// Creates a `&mut UserRef<[T]>` from a raw thin pointer and a slice length.
@@ -412,7 +437,10 @@ where
412
437
/// * The pointed-to range does not fit in the address space
413
438
/// * The pointed-to range is not in user memory
414
439
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
+ }
416
444
}
417
445
418
446
/// Obtain a raw pointer to the first element of this user slice.
@@ -437,13 +465,12 @@ where
437
465
/// This function panics if the destination doesn't have the same size as
438
466
/// the source. This can happen for dynamically-sized types such as slices.
439
467
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)
446
470
}
471
+ // SAFETY: We reserve enough space above.
472
+ unsafe { dest. set_len ( self . len ( ) ) } ;
473
+ self . copy_to_enclave ( & mut dest[ ..] ) ;
447
474
}
448
475
449
476
/// Copies the value from user memory into a vector in enclave memory.
0 commit comments