wasm-tools icon indicating copy to clipboard operation
wasm-tools copied to clipboard

What is `realloc-via-memory-grow` on `wasm-tools component new` supposed to do?

Open arjunr2 opened this issue 3 weeks ago • 1 comments

When trying to use wasm-tools component new, I noticed the realloc-via-memory-grow option.

My intuition was that this provides a default cabi_realloc implementation if the module doesn't contain one. However, I notice that it basically errors out if no cabi_realloc export is provided? (and if one is provided, it just uses that). Am I correct on the purpose of this flag? Not sure exactly what it's supposed to do.

Reproducing the Setup

Here is a simple test for what I'm trying:

A component string.wat

(module
    (type (;0;) (func (param i32 i32 i32)))
    (type (;1;) (func (result i32)))
    (type (;2;) (func (param i32 i64) (result i64)))
    (import "component:test-package/env" "reverse-string" (func $reverse_import (type 0)))
    (global $bump (mut i32) (i32.const 4096))
    (func $main (export "main") (param i32 i32) (result i32)
        (local $str_ptr i32)
        i32.const 100
        local.set $str_ptr
        local.get 0
        local.get 1
        local.get $str_ptr
        call $reverse_import
        local.get $str_ptr
    )
    (memory (export "memory") 1)
)

A WIT file string.wit

package root:component;

world root {
  import component:test-package/env;

  export main: func(x: string) -> string;
}
package component:test-package {
  interface env {
    reverse-string: func(s: string) -> string;
  }
}

Running the following commands:

wasm-tools component embed -t -o string-cc.wat string.wat string.wit
wasm-tools component new --realloc-via-memory-grow -t -o string-component.wat string-cc.wat

produces this:

error: failed to encode a component from module

Caused by:
    0: module does not export a function named `cabi_realloc`

arjunr2 avatar Dec 17 '25 20:12 arjunr2

Ah this is a bit of an obscure option related to the wasip1-to-wasip2 adapter that comes with wasm-component-ld (e.g. wasi_snapshot_preview1.{command,reactor}.wasm. This flag specifically says that the memory allocated for the adapter, if it needs it, is done via memory.grow instead of importing cabi_realloc from the main module, which is the default.

Probably a bad name for this option, but it doesn't synthesize cabi_realloc and is only relevant for adapters like the wasip1-to-wasip2 adapter.

alexcrichton avatar Dec 18 '25 16:12 alexcrichton