and has not been observed in other scenarios. When encountering this bug, WebAssembly memory accesses will produce a segmentation fault. You can avoid encountering this bug by setting `InstanceLimits::memory_pages` to 1x1k, which is the maximum supported size of a WebAssembly page. _________________________________ You may be interested in the details of the bug. When setting the `InstanceLimits::memory_pages` of the `wasmtime` crate, Wasmtime is configured to allocate WebAssembly memory in `pooling` allocator mode. In pooling allocator mode, the size of each WebAssembly page is dynamically calculated based on the size of the block of memory requested by the WebAssembly module. When the `InstanceLimits::memory_pages` setting is configured to 1x1k, Wasmtime cannot dynamically calculate the size of a WebAssembly page because each WebAssembly page is only 1k in size. When Wasmtime cannot dynamically calculate the size of a WebAssembly page, the size of each WebAssembly page cannot be larger than 1k, so Wasmmite cannot allocate a WebAssembly page using the pooling allocator. In this scenario, Wasmtime cannot create a virtual memory mapping for a WebAssembly page, so WebAssembly memory accesses cannot be performed with a virtual memory mapping. Consequently, a segmentation fault will be triggered when attempting to access a WebAssembly page. You can avoid encountering this bug by setting the
Summary
As mentioned above, when WebAssembly memory accesses are performed with a virtual memory mapping, a segmentation fault occurs. To avoid this bug, set the `InstanceLimits::memory_pages` to 1x1k, which is the maximum supported size of a WebAssembly page.
The `InstanceLimits::pages` setting of the `wasmtime` crate
When the `InstanceLimits::memory_pages` setting is configured to 1x1k, Wasmtime cannot dynamically calculate the size of a WebAssembly page because each WebAssembly page is only 1k in size. When Wasmtime cannot dynamically calculate the size of a WebAssembly page, the size of each WebAssembly page cannot be larger than 1k, so Wasmmite cannot allocate a WebAssembly page using the pooling allocator. In this scenario, Wasmtime cannot create a virtual memory mapping for a WebAssembly page, so WebAssembly memory accesses cannot be performed with a virtual memory mapping. Consequently, a segmentation fault will be triggered when attempting to access a WebAssembly page. To avoid triggering this bug, you should set the `InstanceLimits::memory_pages` to 1x1k and set it as high as possible without exceeding the maximum supported size of a WebAssembly page.
Potential solutions:
Moving the `InstanceLimits::memory_pages` setting to a larger value before running your program is one potential solution.
Alternatively, you can increase the size of the page allocator with a custom allocator. This will allow Wasmmite to allocate WebAssembly pages in the pooling allocator mode with a greater granularity than 1k. To use a custom allocator for WebAssembly memory allocation, you can use the following snippet:
```rust
let mut ptr: Vec
Memory safety checks in WebAssembly
The WebAssembly specification includes a set of memory safety checks that ensure that WebAssembly code does not produce unintended or undefined behavior. These checks are implemented as a series of assertions in the WebAssembly binary format and verified during memory management. These assertions can be disabled by setting `DisableMemorySafetyChecks = true` when compiling a module.
Timeline
Published on: 11/10/2022 20:15:00 UTC
Last modified on: 11/16/2022 02:42:00 UTC