Skip to content

Commit

Permalink
Merge branch 'tetratelabs:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
gaukas authored Dec 14, 2023
2 parents 11b8a0a + a6fea91 commit 4cc2f47
Show file tree
Hide file tree
Showing 33 changed files with 740 additions and 204 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ clean: ## Ensure a clean build
fuzz_timeout_seconds ?= 10
.PHONY: fuzz
fuzz:
@cd internal/integration_test/fuzz && cargo test
@cd internal/integration_test/fuzz && cargo fuzz run no_diff --sanitizer=none -- -rss_limit_mb=8192 -max_total_time=$(fuzz_timeout_seconds)
@cd internal/integration_test/fuzz && cargo fuzz run memory_no_diff --sanitizer=none -- -rss_limit_mb=8192 -max_total_time=$(fuzz_timeout_seconds)
@cd internal/integration_test/fuzz && cargo fuzz run validation --sanitizer=none -- -rss_limit_mb=8192 -max_total_time=$(fuzz_timeout_seconds)
Expand Down
7 changes: 7 additions & 0 deletions cmd/wazero/wazero.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/experimental/gojs"
"github.com/tetratelabs/wazero/experimental/logging"
"github.com/tetratelabs/wazero/experimental/opt"
"github.com/tetratelabs/wazero/experimental/sock"
"github.com/tetratelabs/wazero/experimental/sysfs"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
Expand Down Expand Up @@ -161,6 +162,10 @@ func doRun(args []string, stdOut io.Writer, stdErr logging.Writer) int {
flags.BoolVar(&useInterpreter, "interpreter", false,
"Interprets WebAssembly modules instead of compiling them into native code.")

var useOptimizingCompiler bool
flags.BoolVar(&useOptimizingCompiler, "optimizing-compiler", false,
"[Experimental] Compiles WebAssembly modules using the optimizing compiler.")

var envs sliceFlag
flags.Var(&envs, "env", "key=value pair of environment variable to expose to the binary. "+
"Can be specified multiple times.")
Expand Down Expand Up @@ -269,6 +274,8 @@ func doRun(args []string, stdOut io.Writer, stdErr logging.Writer) int {
var rtc wazero.RuntimeConfig
if useInterpreter {
rtc = wazero.NewRuntimeConfigInterpreter()
} else if useOptimizingCompiler {
rtc = opt.NewRuntimeConfigOptimizingCompiler()
} else {
rtc = wazero.NewRuntimeConfig()
}
Expand Down
6 changes: 6 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
experimentalsys "github.com/tetratelabs/wazero/experimental/sys"
"github.com/tetratelabs/wazero/internal/engine/compiler"
"github.com/tetratelabs/wazero/internal/engine/interpreter"
"github.com/tetratelabs/wazero/internal/engine/wazevo"
"github.com/tetratelabs/wazero/internal/filecache"
"github.com/tetratelabs/wazero/internal/internalapi"
"github.com/tetratelabs/wazero/internal/platform"
Expand Down Expand Up @@ -190,6 +191,11 @@ type runtimeConfig struct {
ensureTermination bool
}

// EnableOptimizingCompiler implements experimental/opt/enabler.EnableOptimizingCompiler.
func (c *runtimeConfig) EnableOptimizingCompiler() {
c.newEngine = wazevo.NewEngine
}

// engineLessConfig helps avoid copy/pasting the wrong defaults.
var engineLessConfig = &runtimeConfig{
enabledFeatures: api.CoreFeaturesV2,
Expand Down
23 changes: 23 additions & 0 deletions experimental/opt/opt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package opt

import (
"runtime"

"github.com/tetratelabs/wazero"
)

type enabler interface {
// EnableOptimizingCompiler enables the optimizing compiler.
// This is only implemented the internal type of wazero.runtimeConfig.
EnableOptimizingCompiler()
}

// NewRuntimeConfigOptimizingCompiler returns a new RuntimeConfig with the optimizing compiler enabled.
func NewRuntimeConfigOptimizingCompiler() wazero.RuntimeConfig {
if runtime.GOARCH != "arm64" {
panic("UseOptimizingCompiler is only supported on arm64")
}
c := wazero.NewRuntimeConfig()
c.(enabler).EnableOptimizingCompiler()
return c
}
20 changes: 20 additions & 0 deletions experimental/opt/opt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package opt_test

import (
"context"
"runtime"
"testing"

"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/experimental/opt"
"github.com/tetratelabs/wazero/internal/testing/require"
)

func TestUseOptimizingCompiler(t *testing.T) {
if runtime.GOARCH != "arm64" {
return
}
c := opt.NewRuntimeConfigOptimizingCompiler()
r := wazero.NewRuntimeWithConfig(context.Background(), c)
require.NoError(t, r.Close(context.Background()))
}
4 changes: 2 additions & 2 deletions imports/wasi_snapshot_preview1/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,15 +464,15 @@ var fdFilestatSetSize = newHostFunc(wasip1.FdFilestatSetSizeName, fdFilestatSetS

func fdFilestatSetSizeFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno {
fd := int32(params[0])
size := uint32(params[1])
size := int64(params[1])

fsc := mod.(*wasm.ModuleInstance).Sys.FS()

// Check to see if the file descriptor is available
if f, ok := fsc.LookupFile(fd); !ok {
return experimentalsys.EBADF
} else {
return f.File.Truncate(int64(size))
return f.File.Truncate(size)
}
}

Expand Down
66 changes: 65 additions & 1 deletion imports/wasi_snapshot_preview1/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,42 @@ func Test_fdFdstatGet_StdioNonblock(t *testing.T) {
}
}

func Test_fdFdstatSetFlagsWithTrunc(t *testing.T) {
tmpDir := t.TempDir()
fileName := "test"

mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().
WithFSConfig(wazero.NewFSConfig().WithDirMount(tmpDir, "/")))
defer r.Close(testCtx)

fsc := mod.(*wasm.ModuleInstance).Sys.FS()
preopen := fsc.RootFS()

fd, errno := fsc.OpenFile(preopen, fileName, experimentalsys.O_RDWR|experimentalsys.O_CREAT|experimentalsys.O_EXCL|experimentalsys.O_TRUNC, 0o600)
require.EqualErrno(t, 0, errno)

// Write the initial text to the file.
f, ok := fsc.LookupFile(fd)
require.True(t, ok)
n, _ := f.File.Write([]byte("abc"))
require.Equal(t, n, 3)

buf, err := os.ReadFile(joinPath(tmpDir, fileName))
require.NoError(t, err)
require.Equal(t, "abc", string(buf))

requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdFdstatSetFlagsName, uint64(fd), uint64(0))
require.Equal(t, `
==> wasi_snapshot_preview1.fd_fdstat_set_flags(fd=4,flags=)
<== errno=ESUCCESS
`, "\n"+log.String())
log.Reset()

buf, err = os.ReadFile(joinPath(tmpDir, fileName))
require.NoError(t, err)
require.Equal(t, "abc", string(buf))
}

func Test_fdFdstatSetFlags(t *testing.T) {
tmpDir := t.TempDir() // open before loop to ensure no locking problems.

Expand Down Expand Up @@ -561,6 +597,13 @@ func Test_fdFdstatSetFlags(t *testing.T) {
`, "\n"+log.String()) // FIXME? flags==0 prints 'flags='
log.Reset()

requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdSeekName, uint64(fd), uint64(0), uint64(0), uint64(1024))
require.Equal(t, `
==> wasi_snapshot_preview1.fd_seek(fd=4,offset=0,whence=0)
<== (newoffset=0,errno=ESUCCESS)
`, "\n"+log.String())
log.Reset()

// Without O_APPEND flag, the data is written at the beginning.
writeWazero()
requireFileContent("wazero6789" + "wazero")
Expand All @@ -573,6 +616,16 @@ func Test_fdFdstatSetFlags(t *testing.T) {
`, "\n"+log.String()) // FIXME? flags==1 prints 'flags=APPEND'
log.Reset()

// Restoring the O_APPEND flag should not reset fd offset.
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdTellName, uint64(fd), uint64(1024))
require.Equal(t, `
==> wasi_snapshot_preview1.fd_tell(fd=4,result.offset=1024)
<== errno=ESUCCESS
`, "\n"+log.String())
log.Reset()
offset, _ := mod.Memory().Read(1024, 4)
require.Equal(t, offset, []byte{6, 0, 0, 0})

// with O_APPEND flag, the data is appended to buffer.
writeWazero()
requireFileContent("wazero6789" + "wazero" + "wazero")
Expand Down Expand Up @@ -782,7 +835,7 @@ func Test_fdFilestatSetSize(t *testing.T) {

tests := []struct {
name string
size uint32
size uint64
content, expectedContent []byte
expectedLog string
expectedErrno wasip1.Errno
Expand Down Expand Up @@ -828,6 +881,17 @@ func Test_fdFilestatSetSize(t *testing.T) {
expectedLog: `
==> wasi_snapshot_preview1.fd_filestat_set_size(fd=4,size=106)
<== errno=ESUCCESS
`,
},
{
name: "large size",
content: []byte(""),
expectedContent: []byte(""),
size: math.MaxUint64,
expectedErrno: wasip1.ErrnoInval,
expectedLog: `
==> wasi_snapshot_preview1.fd_filestat_set_size(fd=4,size=-1)
<== errno=EINVAL
`,
},
}
Expand Down
9 changes: 2 additions & 7 deletions internal/engine/wazevo/backend/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@ func NewCompiler(ctx context.Context, mach Machine, builder ssa.Builder) Compile
return newCompiler(ctx, mach, builder)
}

func newCompiler(ctx context.Context, mach Machine, builder ssa.Builder) *compiler {
registerSetDebug := false
if wazevoapi.RegAllocValidationEnabled {
registerSetDebug = wazevoapi.IsHighRegisterPressure(ctx)
}

func newCompiler(_ context.Context, mach Machine, builder ssa.Builder) *compiler {
c := &compiler{
mach: mach, ssaBuilder: builder,
nextVRegID: regalloc.VRegIDNonReservedBegin,
regAlloc: regalloc.NewAllocator(mach.RegisterInfo(registerSetDebug)),
regAlloc: regalloc.NewAllocator(mach.RegisterInfo()),
}
mach.SetCompiler(c)
return c
Expand Down
6 changes: 5 additions & 1 deletion internal/engine/wazevo/backend/isa/arm64/instr.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ func (i *instruction) brLabel() label {
}

// brOffsetResolved is called when the target label is resolved.
func (i *instruction) brOffsetResolved(offset int64) {
func (i *instruction) brOffsetResolve(offset int64) {
i.u2 = uint64(offset)
i.u3 = 1 // indicate that the offset is resolved, for debugging.
}
Expand All @@ -666,6 +666,10 @@ func (i *instruction) asCondBr(c cond, target label, is64bit bool) {
}
}

func (i *instruction) setCondBrTargets(target label) {
i.u2 = uint64(target)
}

func (i *instruction) condBrLabel() label {
return label(i.u2)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@ func TestInstruction_encode(t *testing.T) {
}},
{want: "20000014", setup: func(i *instruction) {
i.asBr(dummyLabel)
i.brOffsetResolved(0x80)
i.brOffsetResolve(0x80)
}},
{want: "01040034", setup: func(i *instruction) {
i.asCondBr(registerAsRegZeroCond(x1VReg), dummyLabel, false)
Expand Down
Loading

0 comments on commit 4cc2f47

Please sign in to comment.