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

[x/programs] migrate wasm runtime to wasmtime and refactor for VM integration #477

Merged
merged 49 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
f23db1f
[x/programs] migrate to wasm runtime to wasmtime
hexfusion Sep 20, 2023
3ebbd38
Implement memory with tests
hexfusion Sep 20, 2023
8e01086
Add guest import examples
hexfusion Sep 20, 2023
7cec29b
Add vm/storage examples
hexfusion Sep 20, 2023
eb2c03f
Remove storage mocks
hexfusion Sep 20, 2023
8ed6774
Remove hard coded guest imports
hexfusion Sep 20, 2023
c7a8a34
Update token example to use new runtime
hexfusion Sep 20, 2023
c76d204
Implement guest export client
hexfusion Sep 20, 2023
2303f5c
Add config builder
hexfusion Sep 20, 2023
2b6ce89
React to new implementations
hexfusion Sep 20, 2023
7cf7f75
Implement meter
hexfusion Sep 20, 2023
fc80112
Remove failing tests/examples for now
hexfusion Sep 20, 2023
b79d4af
Fix lints
hexfusion Sep 20, 2023
dd620f3
Add Wasi mod as const
hexfusion Sep 20, 2023
3bf8905
Improve docs around host function imports
hexfusion Sep 20, 2023
d50618c
Add meter and runtime stop tests
hexfusion Sep 20, 2023
9f52536
Add licenses
hexfusion Sep 20, 2023
3de881a
Add precompile function
hexfusion Sep 20, 2023
61d8adb
Add precompile bench
hexfusion Sep 20, 2023
7d77550
Remove cache from precompiled bench
hexfusion Sep 20, 2023
2e68363
nit
hexfusion Sep 20, 2023
d2d2198
Add token program example
hexfusion Sep 20, 2023
579fce5
wip
hexfusion Sep 25, 2023
b14a5a5
remove wasi
hexfusion Oct 3, 2023
949a81d
wip
hexfusion Oct 3, 2023
36c7837
Add state import
hexfusion Oct 3, 2023
3d6fa55
Cleanup token
hexfusion Oct 3, 2023
81541cc
Recompile and update tests
hexfusion Oct 3, 2023
c15bba9
add optimizations that reduce binary size
hexfusion Oct 3, 2023
2691f72
Remove bulk memory from token tests
hexfusion Oct 3, 2023
b363a8b
Add runtime import factory
hexfusion Oct 4, 2023
45382ff
Rename state import module as pstate
hexfusion Oct 4, 2023
df816a7
Add counter program to program example
hexfusion Oct 4, 2023
98837de
Update runtime to use SupportedImports type
hexfusion Oct 4, 2023
cd4b1f0
Update token example
hexfusion Oct 4, 2023
149f568
Cleanup logging
hexfusion Oct 4, 2023
1290d2c
More log cleanup
hexfusion Oct 4, 2023
9aa4c77
Cargo fmt
hexfusion Oct 4, 2023
1b0d4be
Cleanup
hexfusion Oct 4, 2023
2b32009
Merge branch 'main' into wasmtime
hexfusion Oct 4, 2023
7ed5c49
Add RunShort benchmark for token
hexfusion Oct 5, 2023
7656d94
Add max wasm stack test
hexfusion Oct 5, 2023
61af5b1
Cleanup and nits
hexfusion Oct 5, 2023
fa1b0ac
Cleanup storage
hexfusion Oct 6, 2023
e045b56
Define defaults for all configs
hexfusion Oct 6, 2023
7fdaff5
Remove unused file
hexfusion Oct 6, 2023
8eb9122
Document opt-level flag for release profile
hexfusion Oct 6, 2023
574fbbd
Merge branch 'main' into wasmtime
hexfusion Oct 6, 2023
14857d7
Remove todo
hexfusion Oct 6, 2023
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
Prev Previous commit
Next Next commit
Fix lints
Signed-off-by: Sam Batschelet <sam.batschelet@avalabs.org>
  • Loading branch information
hexfusion committed Oct 3, 2023
commit b79d4af7037fa4bbdc250dfeea14d7591769ff0f
10 changes: 9 additions & 1 deletion x/programs/examples/imports/program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,16 @@ func (i *Import) invokeProgramFn(
// spend the maximum number of units allowed for this call
hexfusion marked this conversation as resolved.
Show resolved Hide resolved
i.meter.Spend(maxUnits)

cfg, err := runtime.NewConfigBuilder(maxUnits).Build()
if err != nil {
i.log.Error("failed to create runtime config",
zap.Error(err),
)
return -1
}

// create a new runtime for the program to be invoked
rt := runtime.New(i.log, runtime.NewConfigBuilder(maxUnits).Build(), i.imports)
rt := runtime.New(i.log, cfg, i.imports)
err = rt.Initialize(context.Background(), programWasmBytes)
if err != nil {
i.log.Error("failed to initialize runtime",
Expand Down
16 changes: 9 additions & 7 deletions x/programs/examples/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/hypersdk/x/programs/examples/imports/hashmap"
"github.com/ava-labs/hypersdk/x/programs/runtime"
"github.com/ava-labs/hypersdk/x/programs/utils"
Expand Down Expand Up @@ -41,12 +42,13 @@ func TestTokenProgram(t *testing.T) {
imports := make(runtime.Imports)
imports["map"] = hashmap.New(log, db)

cfg := runtime.NewConfigBuilder(maxUnits).
cfg, err := runtime.NewConfigBuilder(maxUnits).
WithBulkMemory(true).
WithLimitMaxMemory(17 * 64 * 1024). // 17 pages
WithLimitMaxMemory(17 * 64 * units.KiB). // 17 pages
Build()
require.NoError(err)
program := NewToken(log, tokenProgramBytes, cfg, imports)
err := program.Run(context.Background())
err = program.Run(context.Background())
require.NoError(err)
}

Expand All @@ -63,15 +65,15 @@ func BenchmarkTokenWazeroProgram(b *testing.B) {
b.Run("benchmark_token_program", func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
cfg := runtime.NewConfigBuilder(maxUnits).
cfg, err := runtime.NewConfigBuilder(maxUnits).
WithBulkMemory(true).
WithLimitMaxMemory(17 * 64 * 1024). // 17 pages
WithLimitMaxMemory(17 * 64 * units.KiB). // 17 pages
WithDefaultCache(true).
Build()

require.NoError(err)
program := NewToken(log, tokenProgramBytes, cfg, imports)
b.StartTimer()
err := program.Run(context.Background())
err = program.Run(context.Background())
require.NoError(err)
}
})
Expand Down
9 changes: 6 additions & 3 deletions x/programs/runtime/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (b *builder) WithDefaultCache(enabled bool) *builder {
return b
}

func (b *builder) Build() *Config {
func (b *builder) Build() (*Config, error) {
cfg := defaultWasmtimeConfig()
if b.maxWasmStack == 0 {
b.maxWasmStack = defaultMaxWasmStack
Expand All @@ -137,7 +137,10 @@ func (b *builder) Build() *Config {
cfg.SetWasmSIMD(b.simd)
cfg.SetProfiler(b.profilingStrategy)
if b.defaultCache {
cfg.CacheConfigLoadDefault()
err := cfg.CacheConfigLoadDefault()
if err != nil {
return nil, err
}
}

if b.limitMaxMemory == 0 {
Expand All @@ -154,7 +157,7 @@ func (b *builder) Build() *Config {
limitMaxMemories: 1,
compileStrategy: b.compileStrategy,
meterMaxUnits: b.meterMaxUnits,
}
}, nil
}

// non-configurable defaults
Expand Down
33 changes: 15 additions & 18 deletions x/programs/runtime/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ import (
"github.com/stretchr/testify/require"
)

var (
log = logging.NewLogger(
"",
logging.NewWrappedCore(
logging.Info,
os.Stderr,
logging.Plain.ConsoleEncoder(),
))
)
var log = logging.NewLogger(
"",
logging.NewWrappedCore(
logging.Info,
os.Stderr,
logging.Plain.ConsoleEncoder(),
))

func TestLimitMaxMemory(t *testing.T) {
require := require.New(t)
Expand All @@ -38,9 +36,10 @@ func TestLimitMaxMemory(t *testing.T) {

// wasm defines 2 pages of memory but runtime set max 1 page
maxFee := uint64(1)
cfg := NewConfigBuilder(maxFee).
cfg, err := NewConfigBuilder(maxFee).
WithLimitMaxMemory(1 * 64 * units.KiB). // 1 pages
Build()
require.NoError(err)
runtime := New(log, cfg, nil)
err = runtime.Initialize(context.Background(), wasm)
require.ErrorContains(err, "memory minimum size of 2 pages exceeds memory limits")
Expand All @@ -59,11 +58,12 @@ func TestLimitMaxMemoryGrow(t *testing.T) {
require.NoError(err)

maxFee := uint64(1)
cfg := NewConfigBuilder(maxFee).
cfg, err := NewConfigBuilder(maxFee).
WithLimitMaxMemory(1 * 64 * units.KiB). // 2 pages
Build()
require.NoError(err)
runtime := New(logging.NoLog{}, cfg, nil)
err = runtime.Initialize(context.Background(), []byte(wasm))
err = runtime.Initialize(context.Background(), wasm)
require.NoError(err)

length, err := runtime.Memory().Len()
Expand All @@ -88,11 +88,12 @@ func TestWriteExceedsLimitMaxMemory(t *testing.T) {
require.NoError(err)

maxFee := uint64(1)
cfg := NewConfigBuilder(maxFee).
cfg, err := NewConfigBuilder(maxFee).
WithLimitMaxMemory(1 * 64 * units.KiB). // 2 pages
Build()
require.NoError(err)
runtime := New(logging.NoLog{}, cfg, nil)
err = runtime.Initialize(context.Background(), []byte(wasm))
err = runtime.Initialize(context.Background(), wasm)
require.NoError(err)
maxMemory, err := runtime.Memory().Len()
require.NoError(err)
Expand All @@ -101,7 +102,3 @@ func TestWriteExceedsLimitMaxMemory(t *testing.T) {
err = runtime.Memory().Write(0, bytes)
require.Error(err, "write memory failed: invalid memory size")
}

func BenchmarkXxx(b *testing.B) {

}
6 changes: 2 additions & 4 deletions x/programs/runtime/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import "github.com/bytecodealliance/wasmtime-go/v12"
var _ Meter = (*meter)(nil)

// NewMeter returns a new meter.
func NewMeter(store *wasmtime.Store, maxUnits uint64) Meter {
store.AddFuel(maxUnits)
func NewMeter(store *wasmtime.Store) Meter {
return &meter{
maxUnits: maxUnits,
store: store,
store: store,
}
}

Expand Down
18 changes: 8 additions & 10 deletions x/programs/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package runtime
import (
"context"
"fmt"
// "time"

"github.com/bytecodealliance/wasmtime-go/v12"
"go.uber.org/zap"
Expand Down Expand Up @@ -42,7 +41,6 @@ type runtime struct {
}

func (r *runtime) Initialize(_ context.Context, programBytes ProgramBytes) (err error) {

r.store = wasmtime.NewStore(wasmtime.NewEngineWithConfig(r.cfg.engine))
r.store.Limiter(
r.cfg.limitMaxMemory,
Expand Down Expand Up @@ -86,9 +84,11 @@ func (r *runtime) Initialize(_ context.Context, programBytes ProgramBytes) (err
return err
}

r.meter = NewMeter(r.store, r.cfg.meterMaxUnits)

// start := time.Now()
r.meter = NewMeter(r.store)
err = r.meter.AddUnits(r.cfg.meterMaxUnits)
if err != nil {
return err
}

imports := getRegisteredImportModules(r.mod.Imports())
// register host functions exposed to the guest (imports)
Expand All @@ -109,8 +109,6 @@ func (r *runtime) Initialize(_ context.Context, programBytes ProgramBytes) (err
}
}

// r.log.Info("time taken to register host functions", zap.Int("ms", int(time.Since(start).Milliseconds())))

// instantiate the module with all of the imports defined by the linker
r.inst, err = link.Instantiate(r.store, r.mod)
if err != nil {
Expand All @@ -122,7 +120,7 @@ func (r *runtime) Initialize(_ context.Context, programBytes ProgramBytes) (err

func getRegisteredImportModules(importTypes []*wasmtime.ImportType) []string {
u := map[string]bool{}
var imports []string
imports := make([]string, len(importTypes))
for _, t := range importTypes {
mod := t.Module()
if u[mod] {
Expand All @@ -134,7 +132,7 @@ func getRegisteredImportModules(importTypes []*wasmtime.ImportType) []string {
return imports
}

func (r *runtime) Call(ctx context.Context, name string, params ...interface{}) ([]uint64, error) {
func (r *runtime) Call(_ context.Context, name string, params ...interface{}) ([]uint64, error) {
var api *wasmtime.Func

switch name {
Expand Down Expand Up @@ -172,7 +170,7 @@ func (r *runtime) Meter() Meter {
return r.meter
}

func (r *runtime) Stop(ctx context.Context) error {
func (r *runtime) Stop(_ context.Context) error {
r.store.SetEpochDeadline(0)
return nil
}
6 changes: 4 additions & 2 deletions x/programs/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"github.com/ava-labs/hypersdk/state"
)

var _ state.Mutable = &testDB{}
var _ state.Immutable = &testDB{}
var (
_ state.Mutable = &testDB{}
_ state.Immutable = &testDB{}
)

type testDB struct {
db *memdb.Database
Expand Down