rustwasm / team

A point of coordination for all things Rust and WebAssembly
MIT License
1.45k stars 59 forks source link

Memory initial size must be <= max size #247

Closed jordy25519 closed 5 years ago

jordy25519 commented 5 years ago

I'm compiling some minimal rust code to wasm and am getting some errors regarding initial vs. maximum memory allocation when running wasm-validate e.g. 0000041: error: memory initial size must be <= max size. I don't understand how I can increase maximum memory pages or why/where/how the initial request for 16 is made.

Not sure if this is the right place for this but any help would be much appreciated.

The minimal code

#![feature(alloc_error_handler)]
#![feature(core_intrinsics)]
#![feature(alloc)]

#![no_std]

extern crate wee_alloc;

extern crate alloc;
use alloc::vec::Vec;

use core::intrinsics;

#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

#[panic_handler]
#[no_mangle]
pub fn panic(_info: &::core::panic::PanicInfo) -> ! {
    unsafe {
        intrinsics::abort();
    }
}

#[alloc_error_handler]
pub fn oom(_: ::core::alloc::Layout) -> ! {
    unsafe { 
        intrinsics::abort();
    }
}

#[no_mangle]
pub extern "C" fn do_something() {
    // This `Vec` will allocate memory through `GLOBAL` above
    let mut v: Vec<u8> = Vec::new();
    v.push(1u8);
    v.resize(10 as usize, 0u8);
}

Running wasm-objdump -x </path/to/.wasm> produces: A memory section like this

Memory[1]:
 - memory[0] pages: initial=17

Can someone explain why 17 (pages) are allocated in this case? I don't understand how this works.

Environment

nightly-x86_64-apple-darwin (default)
rustc 1.33.0-nightly (c2d381d39 2019-01-10)
CryZe commented 5 years ago

That's 1 MiB of stack + data sections

fitzgen commented 5 years ago

What is the max memory size? I'm confused why there even is a max being set.

jordy25519 commented 5 years ago

That's 1 MiB of stack + data sections

So is it correct to say that 16 pages is the default initial allocation for stack and data in this case? If so, how can I achieve the above vector assignment using the stack only (i.e. without increasing the initial allocation above 16)?

What is the max memory size? I'm confused why there even is a max being set.

Turns out a post processing tool was improperly handling the memory section, I've opened a separate issue there to track it: https://github.com/paritytech/wasm-utils/issues/105

jordy25519 commented 5 years ago

Actually, it seems the root issue was a regression upstream in rustc(?)

Bad Environment

❯ rustc -V
rustc 1.33.0-nightly (68fe5182c 2019-01-05)
❯ cargo -V
cargo 1.33.0-nightly (34320d212 2019-01-03)

Initializes 17 memory pages (1 we actually requested + 16 from somewhere else...)

> wasm-objdump -x -j memory /path/to/example.wasm
Memory[1]:
 - memory[0] pages: initial=17

Fixed in this Environment (latest nightly at time of writing)

❯ rustc -V
rustc 1.33.0-nightly (ceb251214 2019-01-16)

❯ cargo -V
cargo 1.33.0-nightly (2b4a5f1f0 2019-01-12)

Only allocates 1 page

> wasm-objdump -x -j memory /path/to/example.wasm
Memory[1]:
 - memory[0] pages: initial=1