Skip to content

Commit

Permalink
How to set resource limit in wasmtime-go? bytecodealliance#101
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaly Volkov committed Mar 1, 2023
1 parent 1e19333 commit 99c183f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
20 changes: 20 additions & 0 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,23 @@ func (store *Store) ConsumeFuel(fuel uint64) (uint64, error) {

return uint64(c_remaining), nil
}

// Limiter provides limits for a store. Used by hosts to limit resource
// consumption of instances. Use negative value to keep the default value
// for the limit.
func (store *Store) Limiter(
memorySize int64,
tableElements int64,
instances int64,
tables int64,
memories int64,
) {
C.wasmtime_store_limiter(
store._ptr,
C.int64_t(memorySize),
C.int64_t(tableElements),
C.int64_t(instances),
C.int64_t(tables),
C.int64_t(memories),
)
}
38 changes: 38 additions & 0 deletions store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,41 @@ func TestConsumeFuel(t *testing.T) {
require.NoError(t, err)
require.Equal(t, (add_fuel - consume_fuel), remaining)
}

func TestLimiterMemorySizeFail(t *testing.T) {
engine := NewEngine()
store := NewStore(engine)

store.Limiter(2*64*1024, -1, -1, -1, -1)
wasm, err := Wat2Wasm(`
(module
(memory 3)
)
`)
require.NoError(t, err)

module, err := NewModule(store.Engine, wasm)
require.NoError(t, err)

_, err = NewInstance(store, module, []AsExtern{})
require.Error(t, err, "memory minimum size of 3 pages exceeds memory limits")
}

func TestLimiterMemorySizeSuccess(t *testing.T) {
engine := NewEngine()
store := NewStore(engine)

store.Limiter(4*64*1024, -1, -1, -1, -1)
wasm, err := Wat2Wasm(`
(module
(memory 3)
)
`)
require.NoError(t, err)

module, err := NewModule(store.Engine, wasm)
require.NoError(t, err)

_, err = NewInstance(store, module, []AsExtern{})
require.NoError(t, err)
}

0 comments on commit 99c183f

Please sign in to comment.