|
| 1 | +//! This is a wrapper around the `WasmPtr` abstraction that does not allow deref of address 0 |
| 2 | +//! This is a common assumption in Emscripten code |
| 3 | +
|
| 4 | +use std::{cell::Cell, fmt}; |
| 5 | +pub use wasmer_runtime_core::memory::ptr::Array; |
| 6 | +use wasmer_runtime_core::{ |
| 7 | + memory::{ptr, Memory}, |
| 8 | + types::{ValueType, WasmExternType}, |
| 9 | +}; |
| 10 | + |
| 11 | +#[repr(transparent)] |
| 12 | +pub struct WasmPtr<T: Copy, Ty = ptr::Item>(ptr::WasmPtr<T, Ty>); |
| 13 | + |
| 14 | +unsafe impl<T: Copy, Ty> ValueType for WasmPtr<T, Ty> {} |
| 15 | +impl<T: Copy, Ty> Copy for WasmPtr<T, Ty> {} |
| 16 | + |
| 17 | +impl<T: Copy, Ty> Clone for WasmPtr<T, Ty> { |
| 18 | + fn clone(&self) -> Self { |
| 19 | + Self(self.0.clone()) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +impl<T: Copy, Ty> fmt::Debug for WasmPtr<T, Ty> { |
| 24 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 25 | + write!(f, "{:?}", self.0) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +unsafe impl<T: Copy, Ty> WasmExternType for WasmPtr<T, Ty> { |
| 30 | + type Native = <ptr::WasmPtr<T, Ty> as WasmExternType>::Native; |
| 31 | + |
| 32 | + fn to_native(self) -> Self::Native { |
| 33 | + self.0.to_native() |
| 34 | + } |
| 35 | + fn from_native(n: Self::Native) -> Self { |
| 36 | + Self(ptr::WasmPtr::from_native(n)) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl<T: Copy, Ty> PartialEq for WasmPtr<T, Ty> { |
| 41 | + fn eq(&self, other: &Self) -> bool { |
| 42 | + self.0 == other.0 |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl<T: Copy, Ty> Eq for WasmPtr<T, Ty> {} |
| 47 | + |
| 48 | +impl<T: Copy, Ty> WasmPtr<T, Ty> { |
| 49 | + #[inline(always)] |
| 50 | + pub fn new(offset: u32) -> Self { |
| 51 | + Self(ptr::WasmPtr::new(offset)) |
| 52 | + } |
| 53 | + |
| 54 | + #[inline(always)] |
| 55 | + pub fn offset(self) -> u32 { |
| 56 | + self.0.offset() |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl<T: Copy + ValueType> WasmPtr<T, ptr::Item> { |
| 61 | + #[inline(always)] |
| 62 | + pub fn deref<'a>(self, memory: &'a Memory) -> Option<&'a Cell<T>> { |
| 63 | + if self.0.offset() == 0 { |
| 64 | + None |
| 65 | + } else { |
| 66 | + self.0.deref(memory) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + #[inline(always)] |
| 71 | + pub unsafe fn deref_mut<'a>(self, memory: &'a Memory) -> Option<&'a mut Cell<T>> { |
| 72 | + if self.0.offset() == 0 { |
| 73 | + None |
| 74 | + } else { |
| 75 | + self.0.deref_mut(memory) |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl<T: Copy + ValueType> WasmPtr<T, ptr::Array> { |
| 81 | + #[inline(always)] |
| 82 | + pub fn deref<'a>(self, memory: &'a Memory, index: u32, length: u32) -> Option<&'a [Cell<T>]> { |
| 83 | + if self.0.offset() == 0 { |
| 84 | + None |
| 85 | + } else { |
| 86 | + self.0.deref(memory, index, length) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + #[inline] |
| 91 | + pub unsafe fn deref_mut<'a>( |
| 92 | + self, |
| 93 | + memory: &'a Memory, |
| 94 | + index: u32, |
| 95 | + length: u32, |
| 96 | + ) -> Option<&'a mut [Cell<T>]> { |
| 97 | + if self.0.offset() == 0 { |
| 98 | + None |
| 99 | + } else { |
| 100 | + self.0.deref_mut(memory, index, length) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + #[allow(dead_code)] |
| 105 | + #[inline(always)] |
| 106 | + pub fn get_utf8_string<'a>(self, memory: &'a Memory, str_len: u32) -> Option<&'a str> { |
| 107 | + if self.0.offset() == 0 { |
| 108 | + None |
| 109 | + } else { |
| 110 | + self.0.get_utf8_string(memory, str_len) |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments