/// Expects a (fixed size) Region struct at ptr, which is read. This links to the
/// memory region, which is copied in the second step.
/// Errors if the length of the region exceeds `max_length`.
Expects a (fixed size) Region struct at ptr, which is read. This links to the
memory region, which is copied in the second step.
Errors if the length of the region exceeds `max_length`.
Expects a ( fixed size ) Region struct at ptr which is read . This links to the memory region which is copied in the second step . Errors if the length of the region exceeds ` max_length ` .
pub fn read_region(ctx: &Ctx, ptr: u32, max_length: usize) -> VmResult<Vec<u8>> {
    let region = get_region(ctx, ptr);
    if region.length > to_u32(max_length)? {
        return Err(VmError::region_length_too_big(
            region.length as usize,
            max_length,
        ));
    }
    let memory = ctx.memory(0);
    match WasmPtr::<u8, Array>::new(region.offset).deref(memory, 0, region.length) {
        Some(cells) => {
            // In case you want to do some premature optimization, this shows how to cast a `&'mut [Cell<u8>]` to `&mut [u8]`:
            // https://github.com/wasmerio/wasmer/blob/0.13.1/lib/wasi/src/syscalls/mod.rs#L79-L81
            let len = region.length as usize;
            let mut result = vec![0u8; len];
            for i in 0..len {
                result[i] = cells[i].get();
            }
            Ok(result)
        }
        None => panic!(
            "Error dereferencing region {:?} in wasm memory of size {}. This typically happens when the given pointer does not point to a Region struct.",
            region,
            memory.size().bytes().0
        ),
    }
}
pub fn read_region ( ctx : & Ctx , ptr : u32 , max_length : usize ) -> VmResult < Vec < u8 > > { let region = get_region ( ctx , ptr ) ; if region . length > to_u32 ( max_length ) ? { return Err ( VmError :: region_length_too_big ( region . length as usize , max_length , ) ) ; } let memory = ctx . memory ( 0 ) ; match WasmPtr :: < u8 , Array > :: new ( region . offset ) . deref ( memory , 0 , region . length ) { Some ( cells ) => { // In case you want to do some premature optimization, this shows how to cast a `&'mut [Cell<u8>]` to `&mut [u8]`: // https://github.com/wasmerio/wasmer/blob/0.13.1/lib/wasi/src/syscalls/mod.rs#L79-L81 let len = region . length as usize ; let mut result = vec ! [ 0u8 len ] ; for i in 0 .. len { result [ i ] = cells [ i ] . get ( ) ; } Ok ( result ) } None => panic ! ( " " region memory size ( ) bytes ( ) 0 ) , } }
Expects a (fixed size) Region struct at ptr, which is read.
Expects a ( fixed size ) Region struct at ptr which is read .
ctx
&Ctx
ptr
u32
max_length
usize