Consolidate methods of memory initialization (#3766)
* Consolidate methods of memory initialization This commit consolidates the few locations that we have which are performing memory initialization. Namely the uffd logic for creating paged memory as well as the memfd logic for creating a memory image now share an implementation to avoid duplicating bounds-checks or other validation conditions. The main purpose of this commit is to fix a fuzz-bug where a multiplication overflowed. The overflow itself was benign but it seemed better to fix the overflow in only one place instead of multiple. The overflow in question is specifically when an initializer is checked to be statically out-of-bounds and multiplies a memory's minimum size by the wasm page size, returning the result as a `u64`. For memory64-memories of size `1 << 48` this multiplication will overflow. This was actually a preexisting bug with the `try_paged_init` function which was copied for memfd, but cropped up here since memfd is used more often than paged initialization. The fix here is to skip validation of the `end` index if the size of memory is `1 << 64` since if the `end` index can be represented as a `u64` then it's in-bounds. This is somewhat of an esoteric case, though, since a memory of minimum size `1 << 64` can't ever exist (we can't even ask the os for that much memory, and even if we could it would fail). * Fix memfd test * Fix some tests * Remove InitMemory enum * Add an `is_segmented` helper method * More clear variable name * Make arguments to `init_memory` more descriptive
This commit is contained in:
@@ -263,6 +263,7 @@ unsafe fn initialize_wasm_page(
|
||||
) -> Result<()> {
|
||||
// Check for paged initialization and copy the page if present in the initialization data
|
||||
if let MemoryInitialization::Paged { map, .. } = &instance.module.memory_initialization {
|
||||
let memory_index = instance.module().memory_index(memory_index);
|
||||
let pages = &map[memory_index];
|
||||
|
||||
let pos = pages.binary_search_by_key(&(page_index as u64), |k| k.0);
|
||||
|
||||
Reference in New Issue
Block a user