multiversx_chain_vm/
mem_conv.rs

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use multiversx_chain_vm_executor::{MemLength, MemPtr};

pub fn with_mem_ptr<F, R>(bytes: &[u8], f: F) -> R
where
    F: FnOnce(MemPtr, MemLength) -> R,
{
    f(bytes.as_ptr() as MemPtr, bytes.len() as MemLength)
}

pub fn with_mem_ptr_mut<F, R>(bytes: &mut [u8], f: F) -> R
where
    F: FnOnce(MemPtr, MemLength) -> R,
{
    f(bytes.as_ptr() as MemPtr, bytes.len() as MemLength)
}

/// Interprets an offset and length (both isize) as a byte slice.
///
/// # Safety
///
/// Should only be called with arguments that originate from `with_mem_ptr`.
pub unsafe fn with_bytes<F, R>(offset: MemPtr, length: MemLength, f: F) -> R
where
    F: FnOnce(&[u8]) -> R,
{
    let bytes = std::ptr::slice_from_raw_parts(offset as *const u8, length as usize);
    f(&*bytes)
}

/// Interprets an offset and length (both isize) as a mutable byte slice.
///
/// # Safety
///
/// Should only be called with arguments that originate from `with_mem_ptr_mut`.
pub unsafe fn with_bytes_mut<F, R>(offset: MemPtr, length: MemLength, f: F) -> R
where
    F: FnOnce(&mut [u8]) -> R,
{
    let bytes = std::ptr::slice_from_raw_parts_mut(offset as *mut u8, length as usize);
    f(&mut *bytes)
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_mem_ptr_conversion() {
        assert_mem_conv_sound(vec![]);
        assert_mem_conv_sound(vec![1]);
        assert_mem_conv_sound(vec![1, 2, 3]);
    }

    fn assert_mem_conv_sound(data: Vec<u8>) {
        let cloned = with_mem_ptr(data.as_slice(), |offset, length| unsafe {
            with_bytes(offset, length, |bytes| bytes.to_vec())
        });
        assert_eq!(data, cloned);
    }

    #[test]
    fn test_mem_ptr_mut() {
        let mut data = vec![1, 2, 3];
        with_mem_ptr_mut(data.as_mut_slice(), |offset, length| unsafe {
            with_bytes_mut(offset, length, |bytes| {
                for b in bytes {
                    *b += 1;
                }
            })
        });
        assert_eq!(data, vec![2, 3, 4]);
    }
}