Makes memory limit configurable and a compile error (#419)

This allows users to reduce the memory limit per module below 4 Gi. This
is often needed because Wasm routinely leaves off the max, which implies
spec max (4 Gi). This uses Ki Gi etc in error messages because the spec
chooses to, though we can change to make it less awkward.

This also fixes an issue where we instantiated an engine inside config.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2022-03-31 08:57:28 +08:00
committed by GitHub
parent 81c2414fff
commit 8f461f6f12
26 changed files with 410 additions and 208 deletions

View File

@@ -1,6 +1,7 @@
package wazero
import (
"context"
"io"
"math"
"testing"
@@ -11,7 +12,72 @@ import (
internalwasm "github.com/tetratelabs/wazero/internal/wasm"
)
func TestRuntimeConfig_Features(t *testing.T) {
func TestRuntimeConfig(t *testing.T) {
tests := []struct {
name string
with func(*RuntimeConfig) *RuntimeConfig
expected *RuntimeConfig
}{
{
name: "WithContext",
with: func(c *RuntimeConfig) *RuntimeConfig {
return c.WithContext(context.TODO())
},
expected: &RuntimeConfig{
ctx: context.TODO(),
},
},
{
name: "WithContext - nil",
with: func(c *RuntimeConfig) *RuntimeConfig {
return c.WithContext(nil) //nolint
},
expected: &RuntimeConfig{
ctx: context.Background(),
},
},
{
name: "WithMemoryMaxPages",
with: func(c *RuntimeConfig) *RuntimeConfig {
return c.WithMemoryMaxPages(1)
},
expected: &RuntimeConfig{
memoryMaxPages: 1,
},
},
{
name: "mutable-global",
with: func(c *RuntimeConfig) *RuntimeConfig {
return c.WithFeatureMutableGlobal(true)
},
expected: &RuntimeConfig{
enabledFeatures: internalwasm.FeatureMutableGlobal,
},
},
{
name: "sign-extension-ops",
with: func(c *RuntimeConfig) *RuntimeConfig {
return c.WithFeatureSignExtensionOps(true)
},
expected: &RuntimeConfig{
enabledFeatures: internalwasm.FeatureSignExtensionOps,
},
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
input := &RuntimeConfig{}
rc := tc.with(input)
require.Equal(t, tc.expected, rc)
// The source wasn't modified
require.Equal(t, &RuntimeConfig{}, input)
})
}
}
func TestRuntimeConfig_FeatureToggle(t *testing.T) {
tests := []struct {
name string
feature internalwasm.Features