Replaces MemorySizer and CompileConfig with RuntimeConfig (#815)

We formerly introduced `MemorySizer` as a way to control capacity independently of size. This was the first and only feature in `CompileConfig`. While possibly used privately, `MemorySizer` has never been used in public GitHub code.

These APIs interfere with how we do caching of compiled modules. Notably, they can change the min or max defined in wasm, which invalidates some constants. This has also had a bad experience, forcing everyone to boilerplate`wazero.NewCompileConfig()` despite that API never being used in open source.

This addresses the use cases in a different way, by moving configuration to `RuntimeConfig` instead. This allows us to remove `MemorySizer` and `CompileConfig`, and the problems with them, yet still retaining functionality in case someone uses it.

* `RuntimeConfig.WithMemoryLimitPages(uint32)`: Prevents memory from growing to 4GB (spec limit) per instance.
  * This works regardless of whether the wasm encodes max or not. If there is no max, it becomes effectively this value.
* `RuntimeConfig.WithMemoryCapacityFromMax(bool)`: Prevents reallocations (when growing).
  * Wasm that never sets max will grow from min to the limit above.

Note: Those who want to change their wasm (ex insert a max where there was none), have to do that externally, ex via compiler settings or post-build transformations such as [wabin](https://github.com/tetratelabs/wabin)

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2022-09-29 08:03:03 +08:00
committed by GitHub
parent 84488768e4
commit 761347db1e
40 changed files with 263 additions and 258 deletions

View File

@@ -6,7 +6,6 @@ import (
"io"
"io/fs"
"math"
"reflect"
"testing"
"testing/fstest"
@@ -33,7 +32,26 @@ func TestRuntimeConfig(t *testing.T) {
enabledFeatures: api.CoreFeaturesV1,
},
},
{
name: "memoryLimitPages",
with: func(c RuntimeConfig) RuntimeConfig {
return c.WithMemoryLimitPages(10)
},
expected: &runtimeConfig{
memoryLimitPages: 10,
},
},
{
name: "memoryCapacityFromMax",
with: func(c RuntimeConfig) RuntimeConfig {
return c.WithMemoryCapacityFromMax(true)
},
expected: &runtimeConfig{
memoryCapacityFromMax: true,
},
},
}
for _, tt := range tests {
tc := tt
@@ -45,46 +63,14 @@ func TestRuntimeConfig(t *testing.T) {
require.Equal(t, &runtimeConfig{}, input)
})
}
}
func TestCompileConfig(t *testing.T) {
mp := func(minPages uint32, maxPages *uint32) (min, capacity, max uint32) {
return 0, 1, 1
}
tests := []struct {
name string
with func(CompileConfig) CompileConfig
expected *compileConfig
}{
{
name: "WithMemorySizer",
with: func(c CompileConfig) CompileConfig {
return c.WithMemorySizer(mp)
},
expected: &compileConfig{memorySizer: mp},
},
{
name: "WithMemorySizer twice",
with: func(c CompileConfig) CompileConfig {
return c.WithMemorySizer(wasm.MemorySizer).WithMemorySizer(mp)
},
expected: &compileConfig{memorySizer: mp},
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
input := &compileConfig{}
rc := tc.with(input).(*compileConfig)
// We cannot compare func, but we can compare reflect.Value
// See https://go.dev/ref/spec#Comparison_operators
require.Equal(t, reflect.ValueOf(tc.expected.memorySizer), reflect.ValueOf(rc.memorySizer))
// The source wasn't modified
require.Equal(t, &compileConfig{}, input)
t.Run("memoryLimitPages invalid panics", func(t *testing.T) {
err := require.CapturePanic(func() {
input := &runtimeConfig{}
input.WithMemoryLimitPages(wasm.MemoryLimitPages + 1)
})
}
require.EqualError(t, err, "memoryLimitPages invalid: 65537 > 65536")
})
}
func TestModuleConfig(t *testing.T) {