Adds Runtime.WithCapacityPages to avoid allocations during runtime. (#514)

`Runtime.WithMemoryCapacityPages` is a function that determines memory
capacity in pages (65536 bytes per page). The inputs are the min and
possibly nil max defined by the module, and the default is to return
the min.

Ex. To set capacity to max when exists:
```golang
c.WithMemoryCapacityPages(func(minPages uint32, maxPages *uint32) uint32 {
	if maxPages != nil {
		return *maxPages
	}
	return minPages
})
```

Note: This applies at compile time, ModuleBuilder.Build or Runtime.CompileModule.

Fixes #500

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2022-04-29 17:54:48 +08:00
committed by GitHub
parent 189b694140
commit 2c03098dba
30 changed files with 532 additions and 217 deletions

View File

@@ -159,7 +159,7 @@ func TestNewModuleBuilder_Build(t *testing.T) {
return r.NewModuleBuilder("").ExportMemory("memory", 1)
},
expected: &wasm.Module{
MemorySection: &wasm.Memory{Min: 1, Max: wasm.MemoryMaxPages},
MemorySection: &wasm.Memory{Min: 1, Cap: 1, Max: wasm.MemoryLimitPages},
ExportSection: []*wasm.Export{
{Name: "memory", Type: wasm.ExternTypeMemory, Index: 0},
},
@@ -171,7 +171,7 @@ func TestNewModuleBuilder_Build(t *testing.T) {
return r.NewModuleBuilder("").ExportMemory("memory", 1).ExportMemory("memory", 2)
},
expected: &wasm.Module{
MemorySection: &wasm.Memory{Min: 2, Max: wasm.MemoryMaxPages},
MemorySection: &wasm.Memory{Min: 2, Cap: 2, Max: wasm.MemoryLimitPages},
ExportSection: []*wasm.Export{
{Name: "memory", Type: wasm.ExternTypeMemory, Index: 0},
},
@@ -183,7 +183,7 @@ func TestNewModuleBuilder_Build(t *testing.T) {
return r.NewModuleBuilder("").ExportMemoryWithMax("memory", 1, 1)
},
expected: &wasm.Module{
MemorySection: &wasm.Memory{Min: 1, Max: 1},
MemorySection: &wasm.Memory{Min: 1, Cap: 1, Max: 1, IsMaxEncoded: true},
ExportSection: []*wasm.Export{
{Name: "memory", Type: wasm.ExternTypeMemory, Index: 0},
},
@@ -195,7 +195,7 @@ func TestNewModuleBuilder_Build(t *testing.T) {
return r.NewModuleBuilder("").ExportMemoryWithMax("memory", 1, 1).ExportMemoryWithMax("memory", 1, 2)
},
expected: &wasm.Module{
MemorySection: &wasm.Memory{Min: 1, Max: 2},
MemorySection: &wasm.Memory{Min: 1, Cap: 1, Max: 2, IsMaxEncoded: true},
ExportSection: []*wasm.Export{
{Name: "memory", Type: wasm.ExternTypeMemory, Index: 0},
},
@@ -362,22 +362,26 @@ func TestNewModuleBuilder_Build(t *testing.T) {
func TestNewModuleBuilder_Build_Errors(t *testing.T) {
tests := []struct {
name string
input func(Runtime) ModuleBuilder
input func(*RuntimeConfig) ModuleBuilder
expectedErr string
}{
{
name: "memory max > limit",
input: func(r Runtime) ModuleBuilder {
return r.NewModuleBuilder("").ExportMemory("memory", math.MaxUint32)
name: "memory min > limit", // only one test to avoid duplicating tests in module_test.go
input: func(cfg *RuntimeConfig) ModuleBuilder {
return NewRuntimeWithConfig(cfg).NewModuleBuilder("").
ExportMemory("memory", math.MaxUint32)
},
expectedErr: "memory[memory] min 4294967295 pages (3 Ti) > max 65536 pages (4 Gi)",
expectedErr: "memory[memory] min 4294967295 pages (3 Ti) over limit of 65536 pages (4 Gi)",
},
{
name: "memory min > limit",
input: func(r Runtime) ModuleBuilder {
return r.NewModuleBuilder("").ExportMemoryWithMax("memory", 1, math.MaxUint32)
name: "memory cap < min", // only one test to avoid duplicating tests in module_test.go
input: func(cfg *RuntimeConfig) ModuleBuilder {
cfg = cfg.WithMemoryCapacityPages(func(minPages uint32, maxPages *uint32) uint32 {
return 1
})
return NewRuntimeWithConfig(cfg).NewModuleBuilder("").ExportMemory("memory", 2)
},
expectedErr: "memory[memory] max 4294967295 pages (3 Ti) outside range of 65536 pages (4 Gi)",
expectedErr: "memory[memory] capacity 1 pages (64 Ki) less than minimum 2 pages (128 Ki)",
},
}
@@ -385,7 +389,7 @@ func TestNewModuleBuilder_Build_Errors(t *testing.T) {
tc := tt
t.Run(tc.name, func(t *testing.T) {
_, e := tc.input(NewRuntime()).Build(testCtx)
_, e := tc.input(NewRuntimeConfig()).Build(testCtx)
require.EqualError(t, e, tc.expectedErr)
})
}