chasm icon indicating copy to clipboard operation
chasm copied to clipboard

Possible to limit resource usage?

Open Gatgeagent opened this issue 10 months ago • 1 comments

Hi!

Does Chasm provide a way to limit memory usage (e.g. like Chicory: https://chicory.dev/docs/advanced/memory) and limit CPU usage (e.g. like Chicory https://chicory.dev/docs/usage/cpu)?

Gatgeagent avatar Feb 26 '25 00:02 Gatgeagent

Memory Usage: Yes

Chasm respects the memory limits defined in a wasm binary, for example:

(module
  (memory (export "memory") 1 2)

In the above file it describes a memory with an initial size of 1 page and a maximum size of 2 pages, where 1 page = 64 KiB (65536 bytes). So chasm would allocate an array of 65536 bytes when you instantiate your module and this could grow to a maximum of 131072 bytes if your program asked for it.

You can also create memories through chasms api and and import them into you wasm binaries:

(import "env" "memory" (memory $memory 1))

val store = ...
val limits = Limits(1u,2u)
val memoryType = MemoryType(limits, SharingStatus.Unshared)
val memory = memory(store, memoryType)

// provide the memory during instantiation as an import

Cpu Usage: Not really but yes if you're keen

The wasm spec doesn't define any way for CPU usage to be controlled, theres some proposals but none with any traction at the moment. However chasm supports HostFunctions as a way for you to inject code into your wasm program, you could create a global int say usage_left which you decrement and check at regular intervals and halt once it hits zero. This would require a fair bit of work though

CharlieTap avatar Mar 05 '25 19:03 CharlieTap