bytecodealliance / wasmtime-go

Go WebAssembly runtime powered by Wasmtime
https://pkg.go.dev/github.com/bytecodealliance/wasmtime-go
Apache License 2.0
788 stars 79 forks source link

How to set resource limit in wasmtime-go? #101

Open zhenjunMa opened 3 years ago

zhenjunMa commented 3 years ago

1. background

In my case, i want to limit the max memory that the *.wasm file can use, if it reached the limit, it can crash by OOM.

it's better for me to limit both stack and heap memory.

2. what i have found

In wasmtime, which is written by rust, i found some api to limit the resource:

cpu limit : https://docs.rs/wasmtime/0.29.0/wasmtime/struct.Config.html#method.consume_fuel stack limit: https://docs.rs/wasmtime/0.29.0/wasmtime/struct.Config.html#method.consume_fuel memory limit: https://docs.rs/wasmtime/0.29.0/wasmtime/struct.Config.html#method.consume_fuel

these features are so cool and meet my needs, i also found a project named lunatic, it build on wasmtime and allow users to set resource limit as the doc said:

image

3. my question

my host environment is written by golang, so i want to use wasmtime-go to load and run wasm instance, i know this project uses CGO to consume the C API of the Wasmtime project, but i can't find how to set resource limit in wasmtime-go API.

any ideas about this?

alexcrichton commented 3 years ago

Thanks for the report! The main type used for this is a ResourceLimiter in the Rust API and the Store::limiter API. At this time though I don't believe those are exposed via the C API (and transitively not into the Go API as well).

I think the first steps to handling this would be filling out the C API for those constructs, and then it could be mirrored into the Go API as well.

zhenjunMa commented 3 years ago

@alexcrichton

Thanks for your reply! Is there currently a planned time to complete the first step? maybe on November or December ? hhhh

I think that supporting wasm's resource restriction is an essential condition for wasm to run in a production environment, and the number of users of the wasmtime-go project should be quite large.

alexcrichton commented 3 years ago

I don't believe anyone is currently signed up specifically for doing this, but if you're interested we're always happy to review PRs for additions to the C API

zhenjunMa commented 3 years ago

OK, although i am new to rust, i will have a try😊

vlkv commented 1 year ago

Hello, any progress here?..

vlkv commented 1 year ago

@alexcrichton Hi, can you give an example of a similar merged PR which adds something to the C API? I need the resource limit feature and would like to try to implement it myself. Similar PR would help me to get started, a lot.

alexcrichton commented 1 year ago

Unfortunately, no, but I would recommend reading over the Rust documentation for this, learning the C API's implementation a bit, and starting there.

vlkv commented 1 year ago

@alexcrichton Hello, If I want to contribute to C API, which sources should I look at? These, right? https://github.com/bytecodealliance/wasmtime/tree/main/crates/c-api

alexcrichton commented 1 year ago

Indeed!

vlkv commented 1 year ago

Can you please take a look at https://github.com/bytecodealliance/wasmtime-go/pull/171 It looks to me that there is no need to implement anything in C API, but I am not completely sure)