Skip to content

Commit

Permalink
hammer tests
Browse files Browse the repository at this point in the history
Signed-off-by: Edoardo Vacchi <[email protected]>
  • Loading branch information
evacchi committed Mar 7, 2024
1 parent e9ad3fa commit e240147
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions internal/integration_test/engine/adhoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2187,6 +2187,7 @@ func testLinking(t *testing.T, r wazero.Runtime) {
ctx := context.Background()
// Instantiate the first module.
mod, err := r.InstantiateWithConfig(ctx, linking1, wazero.NewModuleConfig().WithName("Ms"))
defer mod.Close(ctx)
require.NoError(t, err)
// The second module builds successfully.
m, err := r.CompileModule(ctx, linking2)
Expand Down
42 changes: 37 additions & 5 deletions internal/integration_test/engine/hammer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@ package adhoc

import (
"context"
"sync"
"testing"

"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/experimental/opt"
"github.com/tetratelabs/wazero/internal/platform"
"github.com/tetratelabs/wazero/internal/testing/hammer"
"github.com/tetratelabs/wazero/internal/testing/require"
"github.com/tetratelabs/wazero/sys"
"runtime"
"sync"
"testing"
)

var hammers = map[string]testCase{
// Tests here are similar to what's described in /RATIONALE.md, but deviate as they involve blocking functions.
"close importing module while in use": {f: closeImportingModuleWhileInUse},
"close imported module while in use": {f: closeImportedModuleWhileInUse},
"close importing module while in use": {f: closeImportingModuleWhileInUse},
"close imported module while in use": {f: closeImportedModuleWhileInUse},
"linking a closed module should not segfault": {f: testLinkingHammer},
}

func TestEngineCompiler_hammer(t *testing.T) {
Expand Down Expand Up @@ -125,6 +126,37 @@ func closeModuleWhileInUse(t *testing.T, r wazero.Runtime, closeFn func(imported
requireFunctionCall(t, importing.ExportedFunction("call_return_input"))
}

// testLinkingHammer links two modules where the first exports a table and the second module imports it,
// overwriting one of the table entries with one of its functions.
// The first module exposes a function that invokes the functionref in the table.
// If the functionref belongs to failed or closed module, then the call should not fail.
func testLinkingHammer(t *testing.T, r wazero.Runtime) {
if !platform.CompilerSupported() {
t.Skip()
}
hammer.NewHammer(t, 1, 10).Run(func(name string) {
ctx := context.Background()
// Instantiate the first module.
mod, err := r.InstantiateWithConfig(ctx, linking1, wazero.NewModuleConfig().WithName("Ms"))
defer mod.Close(ctx)

Check failure on line 141 in internal/integration_test/engine/hammer_test.go

View workflow job for this annotation

GitHub Actions / Pre-commit check

SA5001: should check returned error before deferring mod.Close(ctx) (staticcheck)
require.NoError(t, err)
// The second module builds successfully.
m, err := r.CompileModule(ctx, linking2)
require.NoError(t, err)
// The second module instantiates and sets the table[0] field to point to its own $f function.
_, err = r.InstantiateModule(ctx, m, wazero.NewModuleConfig())
// However it traps upon instantiation.
require.Error(t, err)
m.Close(ctx)
// This should not be necessary to fail.
runtime.GC()
// The result is expected to be 0xdead, i.e., the result of linking2.$f.
res, err := mod.ExportedFunction("get table[0]").Call(ctx) // This should not SIGSEGV.
require.NoError(t, err)
require.Equal(t, uint64(0xdead), res[0])
}, func() {})
}

func requireFunctionCall(t *testing.T, fn api.Function) {
res, err := fn.Call(testCtx, 3)
require.NoError(t, err)
Expand Down

0 comments on commit e240147

Please sign in to comment.