Skip to content

Commit

Permalink
How to set resource limit in wasmtime-go? (#101) (#171)
Browse files Browse the repository at this point in the history
* How to set resource limit in wasmtime-go? (#101)

* Using dev version temporarily

---------

Co-authored-by: Vitaly Volkov <[email protected]>
  • Loading branch information
vlkv and Vitaly Volkov authored Mar 2, 2023
1 parent 1e19333 commit 6deb799
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ci/download-wasmtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import glob


version = 'v6.0.0'
version = 'dev'
urls = [
['wasmtime-{}-x86_64-mingw-c-api.zip', 'windows-x86_64'],
['wasmtime-{}-x86_64-linux-c-api.tar.xz', 'linux-x86_64'],
Expand Down
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 6deb799

Please sign in to comment.