summaryrefslogtreecommitdiffstats
path: root/library/std/src/sys/sgx/abi/usercalls/tests.rs
blob: 58b8eb215d73cc97fe29f4342d050ac5ffc5d8e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use super::alloc::User;
use super::alloc::{copy_from_userspace, copy_to_userspace};

#[test]
fn test_copy_to_userspace_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().add(offset), size) };

            // Verify copy
            for byte in 0..size {
                unsafe {
                    assert_eq!(*dst.as_ptr().add(offset + byte), src[byte as usize]);
                }
            }
        }
    }
}

#[test]
fn test_copy_from_userspace_function() {
    let mut dst = [0u8; 100];
    let mut src = User::<[u8]>::uninitialized(100);

    src.copy_from_enclave(&[0u8; 100]);

    for size in 0..48 {
        // For all possible alignment
        for offset in 0..8 {
            // overwrite complete dst
            dst = [0u8; 100];

            // Copy src[0..size] to dst + offset
            unsafe { copy_from_userspace(src.as_ptr().offset(offset), dst.as_mut_ptr(), size) };

            // Verify copy
            for byte in 0..size {
                unsafe {
                    assert_eq!(dst[byte as usize], *src.as_ptr().offset(offset + byte as isize));
                }
            }
        }
    }
}