Invokes the memory allocation function (which is style after `realloc`) with the specified parameters. # Panics This will panic if realloc hasn't been configured for this lowering via its canonical options.
(
&mut self,
old: usize,
old_size: usize,
old_align: u32,
new_size: usize,
)
| 138 | /// This will panic if realloc hasn't been configured for this lowering via |
| 139 | /// its canonical options. |
| 140 | pub fn realloc( |
| 141 | &mut self, |
| 142 | old: usize, |
| 143 | old_size: usize, |
| 144 | old_align: u32, |
| 145 | new_size: usize, |
| 146 | ) -> Result<usize> { |
| 147 | assert!(self.allow_realloc); |
| 148 | |
| 149 | let (component, store) = self.instance.component_and_store_mut(self.store.0); |
| 150 | let instance = self.instance.id().get(store); |
| 151 | let options = &component.env_component().options[self.options]; |
| 152 | let realloc_ty = component.realloc_func_ty(); |
| 153 | let realloc = match options.data_model { |
| 154 | CanonicalOptionsDataModel::Gc {} => unreachable!(), |
| 155 | CanonicalOptionsDataModel::LinearMemory(m) => m.realloc.unwrap(), |
| 156 | }; |
| 157 | let realloc = instance.runtime_realloc(realloc); |
| 158 | |
| 159 | let params = ( |
| 160 | u32::try_from(old)?, |
| 161 | u32::try_from(old_size)?, |
| 162 | old_align, |
| 163 | u32::try_from(new_size)?, |
| 164 | ); |
| 165 | |
| 166 | type ReallocFunc = crate::TypedFunc<(u32, u32, u32, u32), u32>; |
| 167 | |
| 168 | // Invoke the wasm malloc function using its raw and statically known |
| 169 | // signature. |
| 170 | let result = unsafe { |
| 171 | ReallocFunc::call_raw(&mut StoreContextMut(store), &realloc_ty, realloc, params)? |
| 172 | }; |
| 173 | |
| 174 | if result % old_align != 0 { |
| 175 | bail!("realloc return: result not aligned"); |
| 176 | } |
| 177 | let result = usize::try_from(result)?; |
| 178 | |
| 179 | if self |
| 180 | .as_slice_mut() |
| 181 | .get_mut(result..) |
| 182 | .and_then(|s| s.get_mut(..new_size)) |
| 183 | .is_none() |
| 184 | { |
| 185 | bail!("realloc return: beyond end of memory") |
| 186 | } |
| 187 | |
| 188 | Ok(result) |
| 189 | } |
| 190 | |
| 191 | /// Returns a fixed mutable slice of memory `N` bytes large starting at |
| 192 | /// offset `N`, panicking on out-of-bounds. |
no test coverage detected