wasmerio / wasmer-go

🐹🕸️ WebAssembly runtime for Go
https://pkg.go.dev/github.com/wasmerio/wasmer-go
MIT License
2.84k stars 161 forks source link

Insufficient resources: Failed to create memory: Error when allocating memory: Cannot allocate memory (os error 12) #334

Closed Fvoiretryzig closed 2 years ago

Fvoiretryzig commented 2 years ago

Thanks for the bug report!

Describe the bug

When I new an instance many times in go, it will show the error message after I execute instance, err := wasmer.NewInstance(module, importObject)

this is instance err: Insufficient resources: Failed to create memory: Error when allocating memory: Cannot allocate memory (os error 12)

Steps to reproduce

  1. This is my code,
    
    package main

import ( "fmt" wasmer "github.com/wasmerio/wasmer-go/wasmer" "io/ioutil" ) var ( module wasmer.Module store wasmer.Store ) func run() { // Instantiates the module wasiEnvBuilder := wasmer.NewWasiStateBuilder("hello.wasm").CaptureStdout().CaptureStderr() wasiEnv, err := wasiEnvBuilder.Finalize() if err != nil { fmt.Println("this is wasiEnvBuilder.Finalize err!") fmt.Println(err) return } importObject, err := wasiEnv.GenerateImportObject(store, module) if err != nil { fmt.Println("this is wasiEnv.GenerateImportObject err!") fmt.Println(err) return } instance, err := wasmer.NewInstance(module, importObject) if err!=nil { fmt.Println("this is instance err:", err) return } start, err := instance.Exports.GetWasiStartFunction() if err != nil { fmt.Println("this is instance.Exports.GetWasiStartFunction err!") fmt.Println(err) return } start() fmt.Println(string(wasiEnv.ReadStdout())) instance.Close() } func main() { wasmBytes, := ioutil.ReadFile("hello.wasm") store = wasmer.NewStore(wasmer.NewEngine()) module, = wasmer.NewModule(store, wasmBytes) for i:=0; i<100; i++ { fmt.Printf("this is %d times run\n", i) run() } }

2. The wasm file is from [hello.wasm](https://github.com/wasmerio/wasmer/blob/3cb420fe30e0c4f54db40c66ca82ed9e34ba7db5/tests/wasi-wast/wasi/unstable/hello.wasm) in wasmer repository.
3. Then I run with "go run hello.go", it show these error message.

... this is 83 times run Hello, world!

this is 84 times run this is instance err: Insufficient resources: Failed to create memory: Error when allocating memory: Cannot allocate memory (os error 12) this is 85 times run this is instance err: Insufficient resources: Failed to create memory: Error when allocating memory: Cannot allocate memory (os error 12) this is 86 times run this is instance err: Insufficient resources: Failed to create memory: Error when allocating memory: Cannot allocate memory (os error 12) this is 87 times run this is instance err: Insufficient resources: Failed to create memory: Error when allocating memory: Cannot allocate memory (os error 12) this is 88 times run this is instance err: Insufficient resources: Failed to create memory: Error when allocating memory: Cannot allocate memory (os error 12) this is 89 times run this is instance err: Insufficient resources: Failed to create memory: Error when allocating memory: Cannot allocate memory (os error 12) this is 90 times run this is instance err: Insufficient resources: Failed to create memory: Error when allocating memory: Cannot allocate memory (os error 12) this is 91 times run this is instance err: Insufficient resources: Failed to create memory: Error when allocating memory: Cannot allocate memory (os error 12) this is 92 times run this is instance err: Insufficient resources: Failed to create memory: Error when allocating memory: Cannot allocate memory (os error 12) this is 93 times run this is instance err: Insufficient resources: Failed to create memory: Error when allocating memory: Cannot allocate memory (os error 12) this is 94 times run this is instance err: Insufficient resources: Failed to create memory: Error when allocating memory: Cannot allocate memory (os error 12) this is 95 times run this is instance err: Insufficient resources: Failed to create memory: Error when allocating memory: Cannot allocate memory (os error 12) this is 96 times run this is instance err: Insufficient resources: Failed to create memory: Error when allocating memory: Cannot allocate memory (os error 12) this is 97 times run this is instance err: Insufficient resources: Failed to create memory: Error when allocating memory: Cannot allocate memory (os error 12) this is 98 times run this is instance err: Insufficient resources: Failed to create memory: Error when allocating memory: Cannot allocate memory (os error 12) this is 99 times run this is instance err: Insufficient resources: Failed to create memory: Error when allocating memory: Cannot allocate memory (os error 12)

### Expected behavior
I expect every run can output "hello world".
### Actual behavior
It shows that in 84 times run it cannot new an instance correctly.
A clear and concise description of what actually happened.
### Additional context
I modify the example [wasi.rs](https://github.com/wasmerio/wasmer/blob/3cb420fe30e0c4f54db40c66ca82ed9e34ba7db5/examples/wasi.rs) in wasmer repository to test if this case can run correctly in rust.  This is the code I modified,

... for i in 1..100000 { println!("this is {} times run", i); println!("Creating WasiEnv..."); // First, we create the WasiEnv let mut wasi_env = WasiState::new("hello") // .args(&["world"]) // .env("KEY", "Value") .finalize()?;

println!("Instantiating module with WASI imports...");
// Then, we get the import object related to our WASI
// and attach it to the Wasm instance.
let import_object = wasi_env.import_object(&module)?;
let instance = Instance::new(&module, &import_object)?;

println!("Call WASI `_start` function...");
// And we just call the `_start` function!
let start = instance.exports.get_function("_start")?;
start.call(&[])?;

} ...

It can run correctly. 

And I test another example in go. The wasm file I use is [simple.wasm](https://github.com/wasmerio/wasmer-go/blob/586bbfef6447d17456d3160e0b2694cdcf7cde75/examples/appendices/simple.wasm). I abandon the wasiEnv part, just like the code in [simple.go](https://github.com/wasmerio/wasmer-go/blob/586bbfef6447d17456d3160e0b2694cdcf7cde75/examples/appendices/simple.go). I add multiple times loop and add `instance.Close()` after every run. This can also run successfully.

I guess this maybe some problem with the implementation of wasi part in golang? How can I deal with this bug?

The libwasmer.so I used is the latest version v2.2.0(I just cloned from the wasmer repository master branch today.) My system version is as follows,

Linux k8snode01 4.9.253-tegra #1 SMP PREEMPT Mon Jul 26 12:13:06 PDT 2021 aarch64 aarch64 aarch64 GNU/Linux

Distributor ID: Ubuntu Description: Ubuntu 18.04.5 LTS Release: 18.04 Codename: bionic

          total        used        free      shared  buff/cache   available

Mem: 4059240 645880 1043860 29280 2369500 3198392 Swap: 2029616 0 2029616


And my go version is `go version go1.16.13 linux/arm64`.
heyjdp commented 2 years ago

Investigating here: https://github.com/wasmerio/wasmer/issues/2861