Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}