wasmtime-go icon indicating copy to clipboard operation
wasmtime-go copied to clipboard

How to set resource limit in wasmtime-go?

Open zhenjunMa opened this issue 2 years ago • 10 comments

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?

zhenjunMa avatar Sep 14 '21 02:09 zhenjunMa

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.

alexcrichton avatar Sep 14 '21 14:09 alexcrichton

@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.

zhenjunMa avatar Sep 15 '21 09:09 zhenjunMa

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

alexcrichton avatar Sep 15 '21 14:09 alexcrichton

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

zhenjunMa avatar Sep 17 '21 02:09 zhenjunMa

Hello, any progress here?..

vlkv avatar Jan 26 '23 11:01 vlkv

@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.

vlkv avatar Jan 27 '23 12:01 vlkv

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

alexcrichton avatar Jan 27 '23 16:01 alexcrichton

@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

vlkv avatar Mar 01 '23 17:03 vlkv

Indeed!

alexcrichton avatar Mar 01 '23 20:03 alexcrichton

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)

vlkv avatar Mar 01 '23 20:03 vlkv