]> git.lizzy.rs Git - rust.git/commitdiff
Test `copy_to_userspace` function
authorRaoul Strackx <raoul.strackx@fortanix.com>
Tue, 22 Mar 2022 16:34:44 +0000 (17:34 +0100)
committerRaoul Strackx <raoul.strackx@fortanix.com>
Wed, 15 Jun 2022 13:01:42 +0000 (15:01 +0200)
library/std/src/sys/sgx/abi/usercalls/alloc.rs
library/std/src/sys/sgx/abi/usercalls/mod.rs
library/std/src/sys/sgx/abi/usercalls/tests.rs [new file with mode: 0644]

index 4c1f279857f9c4038779dbc6bd3f173e831bac36..4ac27e85f8ba8e51dc40ea1f8e37bac814127ddd 100644 (file)
@@ -317,7 +317,7 @@ pub unsafe fn from_raw_parts(ptr: *mut T, len: usize) -> Self {
 /// * The `dst` pointer is null
 /// * The `src` memory range is not in enclave memory
 /// * The `dst` memory range is not in user memory
-unsafe fn copy_to_userspace(src: *const u8, dst: *mut u8, len: usize) {
+pub(crate) unsafe fn copy_to_userspace(src: *const u8, dst: *mut u8, len: usize) {
     unsafe fn copy_bytewise_to_userspace(src: *const u8, dst: *mut u8, len: usize) {
         unsafe {
             let seg_sel: u16 = 0;
index 2f99abba77667a46de92aef2fc10455b1f54ea7f..79d1db5e1c50db01029ae14b2461cbe927843594 100644 (file)
@@ -6,6 +6,8 @@
 pub(crate) mod alloc;
 #[macro_use]
 pub(crate) mod raw;
+#[cfg(test)]
+mod tests;
 
 use self::raw::*;
 
diff --git a/library/std/src/sys/sgx/abi/usercalls/tests.rs b/library/std/src/sys/sgx/abi/usercalls/tests.rs
new file mode 100644 (file)
index 0000000..cbf7d7d
--- /dev/null
@@ -0,0 +1,30 @@
+use super::alloc::copy_to_userspace;
+use super::alloc::User;
+
+#[test]
+fn test_copy_function() {
+    let mut src = [0u8; 100];
+    let mut dst = User::<[u8]>::uninitialized(100);
+
+    for i in 0..src.len() {
+        src[i] = i as _;
+    }
+
+    for size in 0..48 {
+        // For all possible alignment
+        for offset in 0..8 {
+            // overwrite complete dst
+            dst.copy_from_enclave(&[0u8; 100]);
+
+            // Copy src[0..size] to dst + offset
+            unsafe { copy_to_userspace(src.as_ptr(), dst.as_mut_ptr().offset(offset), size) };
+
+            // Verify copy
+            for byte in 0..size {
+                unsafe {
+                    assert_eq!(*dst.as_ptr().offset(offset + byte as isize), src[byte as usize]);
+                }
+            }
+        }
+    }
+}