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
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use crate::{api::unsafe_buffer, error_hook};
use elrond_wasm::{
    api::{Handle, InvalidSliceError, ManagedBufferApi},
    err_msg,
    types::BoxedBytes,
};

// #[allow(dead_code)]
extern "C" {
    fn mBufferNew() -> i32;
    fn mBufferNewFromBytes(byte_ptr: *const u8, byte_len: i32) -> i32;
    fn mBufferGetLength(mBufferHandle: i32) -> i32;
    fn mBufferGetBytes(mBufferHandle: i32, resultOffset: *mut u8) -> i32;
    fn mBufferGetByteSlice(
        sourceHandle: i32,
        startingPosition: i32,
        sliceLength: i32,
        resultOffset: *mut u8,
    ) -> i32;
    fn mBufferCopyByteSlice(
        sourceHandle: i32,
        startingPosition: i32,
        sliceLength: i32,
        destinationHandle: i32,
    ) -> i32;
    #[cfg(not(feature = "unmanaged-ei"))]
    fn mBufferEq(handle1: i32, handle2: i32) -> i32;
    fn mBufferSetBytes(mBufferHandle: i32, byte_ptr: *const u8, byte_len: i32) -> i32;
    fn mBufferAppend(accumulatorHandle: i32, dataHandle: i32) -> i32;
    fn mBufferAppendBytes(accumulatorHandle: i32, byte_ptr: *const u8, byte_len: i32) -> i32;
}

impl ManagedBufferApi for crate::VmApiImpl {
    #[inline]
    fn mb_new_empty(&self) -> Handle {
        unsafe { mBufferNew() }
    }

    #[inline]
    fn mb_new_from_bytes(&self, bytes: &[u8]) -> Handle {
        unsafe { mBufferNewFromBytes(bytes.as_ptr(), bytes.len() as i32) }
    }

    #[inline]
    fn mb_len(&self, handle: Handle) -> usize {
        unsafe { mBufferGetLength(handle as i32) as usize }
    }

    fn mb_to_boxed_bytes(&self, handle: Handle) -> BoxedBytes {
        unsafe {
            let len = mBufferGetLength(handle);
            let mut res = BoxedBytes::allocate(len as usize);
            if len > 0 {
                let _ = mBufferGetBytes(handle, res.as_mut_ptr());
            }
            res
        }
    }

    fn mb_load_slice(
        &self,
        source_handle: Handle,
        starting_position: usize,
        dest_slice: &mut [u8],
    ) -> Result<(), InvalidSliceError> {
        unsafe {
            let err = mBufferGetByteSlice(
                source_handle,
                starting_position as i32,
                dest_slice.len() as i32,
                dest_slice.as_mut_ptr(),
            );
            if err == 0 {
                Ok(())
            } else {
                Err(InvalidSliceError)
            }
        }
    }

    #[inline]
    fn mb_copy_slice(
        &self,
        source_handle: Handle,
        starting_pos: usize,
        slice_len: usize,
        dest_handle: Handle,
    ) -> Result<(), InvalidSliceError> {
        unsafe {
            let err = mBufferCopyByteSlice(
                source_handle,
                starting_pos as i32,
                slice_len as i32,
                dest_handle,
            );
            if err == 0 {
                Ok(())
            } else {
                Err(InvalidSliceError)
            }
        }
    }

    fn mb_copy_to_slice_pad_right(&self, handle: Handle, destination: &mut [u8]) {
        unsafe {
            let byte_len = mBufferGetLength(handle) as usize;
            if byte_len > destination.len() {
                error_hook::signal_error(err_msg::VALUE_EXCEEDS_SLICE)
            }
            if byte_len > 0 {
                let start_index = destination.len() - byte_len;
                let _ = mBufferGetBytes(handle, destination.as_mut_ptr().add(start_index));
            }
        }
    }

    #[inline]
    fn mb_overwrite(&self, handle: Handle, bytes: &[u8]) {
        unsafe {
            let _ = mBufferSetBytes(handle as i32, bytes.as_ptr(), bytes.len() as i32);
        }
    }

    #[inline]
    fn mb_append(&self, accumulator_handle: Handle, data_handle: Handle) {
        unsafe {
            let _ = mBufferAppend(accumulator_handle as i32, data_handle as i32);
        }
    }

    #[inline]
    fn mb_append_bytes(&self, accumulator_handle: Handle, bytes: &[u8]) {
        unsafe {
            let _ = mBufferAppendBytes(
                accumulator_handle as i32,
                bytes.as_ptr(),
                bytes.len() as i32,
            );
        }
    }

    #[cfg(feature = "unmanaged-ei")]
    fn mb_eq(&self, handle1: Handle, handle2: Handle) -> bool {
        // TODO: might be worth adding a new hook to Arwen for this
        unsafe {
            let len1 = mBufferGetLength(handle1 as i32) as usize;
            let len2 = mBufferGetLength(handle2 as i32) as usize;
            if len1 != len2 {
                return false;
            }
            if len1 == 0 {
                return true;
            }
            let mut bytes1 = BoxedBytes::allocate(len1);
            let mut bytes2 = BoxedBytes::allocate(len2);
            let _ = mBufferGetBytes(handle1, bytes1.as_mut_ptr());
            let _ = mBufferGetBytes(handle2, bytes2.as_mut_ptr());
            bytes1 == bytes2
        }
    }

    #[cfg(not(feature = "unmanaged-ei"))]
    fn mb_eq(&self, handle1: Handle, handle2: Handle) -> bool {
        unsafe { mBufferEq(handle1, handle2) > 0 }
    }
}

pub(crate) unsafe fn unsafe_buffer_load_address(address_handle: Handle) -> *const u8 {
    let unsafe_buffer_ptr = unsafe_buffer::buffer_ptr();
    let _ = mBufferGetBytes(address_handle, unsafe_buffer_ptr);
    unsafe_buffer_ptr
}