bytecodealliance/wasmtime-go

Allocation too large< ~lib/rt/itcms.ts<$Index out of range,~lib/rt.ts

Closed this issue · 4 comments

Hello everyone, I'm a newbie learning webassembly, I just encountered the issue but have been stuck several days.
I'm sorry I posted unrelated questions in this repository because this seems to an issue from my code in assemblyscript.
I will appreciate a lot 🙏 for any helps.
I'm stuck on this when learing web assembly.

basically I have this index.ts file written in assembly script

// The entry file of your WebAssembly module.

class Animal {
  constructor() {}
  name: i32;
  age: i32;
}

export function newAnimal(): i32 {
  let animal = new Animal();
  animal.name = 1;
  animal.age = 2;

  return 1;
}

I compile with the following command

npx  asc assembly/index.ts --target release --use abort=assembly/index/myAbort

However when i check the realease.wat file it has some weird messages

 (memory $0 1)
 (data $0 (i32.const 12) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00(\00\00\00A\00l\00l\00o\00c\00a\00t\00i\00o\00n\00 \00t\00o\00o\00 \00l\00a\00r\00g\00e\00\00\00\00\00")
 (data $1 (i32.const 76) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00 \00\00\00~\00l\00i\00b\00/\00r\00t\00/\00i\00t\00c\00m\00s\00.\00t\00s\00\00\00\00\00\00\00\00\00\00\00\00\00")
 (data $2 (i32.const 144) "\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
 (data $3 (i32.const 176) "\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
 (data $4 (i32.const 204) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00$\00\00\00I\00n\00d\00e\00x\00 \00o\00u\00t\00 \00o\00f\00 \00r\00a\00n\00g\00e\00\00\00\00\00\00\00\00\00")
 (data $5 (i32.const 268) ",\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00\14\00\00\00~\00l\00i\00b\00/\00r\00t\00.\00t\00s\00\00\00\00\00\00\00\00\00")
 (data $6 (i32.const 320) "\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
 (data $7 (i32.const 348) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00\1e\00\00\00~\00l\00i\00b\00/\00r\00t\00/\00t\00l\00s\00f\00.\00t\00s\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
 (data $8 (i32.const 416) "\06\00\00\00 \00\00\00 \00\00\00 \00\00\00\00\00\00\00 \00\00\00 \00\00\00")

I decode this Unicode message and got this error

<(Allocation too large< ~lib/rt/itcms.ts<$Index out of range,~lib/rt.ts@@<~lib/rt/tlsf.ts

I execute this using go-wasmtime and also got the same error

package main

import (
	"encoding/json"
	"fmt"
	"net/http"

	"C"

	"github.com/bytecodealliance/wasmtime-go/v9"
)



func main() {
	// Load the WebAssembly module from file

	// Almost all operations in wasmtime require a contextual `store`
	// argument to share, so create that first
	engine := wasmtime.NewEngine()
	store := wasmtime.NewStore(engine)
	linker := wasmtime.NewLinker(engine)

	vals := map[int32]int32{}

	set := func(k, v int32) {
		vals[k] = v
		fmt.Printf("set: %d, %d\n", k, v)
	}

	get := func(k int32) int32 {
		fmt.Printf("get: %d\n", k)
		return vals[k]
	}

	linker.DefineFunc(store, "kv", "set", set)
	linker.DefineFunc(store, "kv", "get", get)

	module, err := wasmtime.NewModuleFromFile(engine, "./release.wasm")
	check(err)

	// Create the WASI instance
	wasiConfig := wasmtime.NewWasiConfig()
	store.SetWasi(wasiConfig)

	check(err)

	instance, err := linker.Instantiate(store, module)

	check(err)

	mem := instance.GetExport(store, "memory").Memory()
	fmt.Printf("mem = %+v\n", mem)
	ptr := mem.Data(store)
	// Allocate memory
	fmt.Printf("Go: wasm sandbox memory info: size %d, data size %d\n",
		mem.Size(store),
		mem.DataSize(store),
	)
	buf := mem.UnsafeData(store)
	fmt.Printf("Go: wasm sandbox memory content: %s\n", string(buf))

}

func check(e error) {
	if e != nil {
		panic(e)
	}
}

Output

mem = &{val:{store_id:1 index:0}}
Go: wasm sandbox memory info: size 512, data size 33554432
Go: wasm sandbox memory content: <(Allocation too large< ~lib/rt/itcms.ts<$Index out of range,~lib/rt.ts@@<~lib/rt/tlsf.ts 

Thank you a lot if any one can give me instruction on how to resolve this issue.

It looks like you're not actually running any wasm other than instantiating the given module? Otherwise you're printing a raw byte slice as a string where I'm not sure what Go's behavior of that is because the input is unlikely to be utf-8 so it may be skipping all non-printable characters which means you're reading the raw contents of memory. The raw contents are that meaningful outside the context of the guest, so I'm not sure what you're trying to achieve by printing memory?

thanks for the help @alexcrichton . I'm just debugging and learning how to pass a string from a guest program in Assembly Script to a host program in Go.
I don't find many examples around how to do this. I copied the memory inspection code from this https://github.com/tricorder-observability/Starship/blob/d5ed851b1471d86dd27b78066b5b97b36e8c42f6/experimental/wasi-sdk-cpp/picohttpparser/main-wasmtime.go#L172

Yes doing something like passing strings right now is unfortunately not a trivial operation. You'll need to be pretty deep in the system and understand what's going on to do that today. That's the goal of the component-model, however, is to provide much easier-to-use interfaces (in the future, which we're actively working on)

noted. thanks a lot for the help Alex