github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/internal/engine/wazevo/wazevo_test.go (about)

     1  package wazevo
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"testing"
     7  	"unsafe"
     8  
     9  	"github.com/tetratelabs/wazero/api"
    10  	"github.com/tetratelabs/wazero/internal/engine/wazevo/wazevoapi"
    11  	"github.com/tetratelabs/wazero/internal/platform"
    12  	"github.com/tetratelabs/wazero/internal/testing/require"
    13  	"github.com/tetratelabs/wazero/internal/wasm"
    14  )
    15  
    16  var ctx = context.Background()
    17  
    18  func TestMain(m *testing.M) {
    19  	if !platform.CompilerSupported() {
    20  		os.Exit(0)
    21  	}
    22  	os.Exit(m.Run())
    23  }
    24  
    25  func TestNewEngine(t *testing.T) {
    26  	e := NewEngine(ctx, api.CoreFeaturesV1, nil)
    27  	require.NotNil(t, e)
    28  }
    29  
    30  func TestEngine_CompiledModuleCount(t *testing.T) {
    31  	e, ok := NewEngine(ctx, api.CoreFeaturesV1, nil).(*engine)
    32  	require.True(t, ok)
    33  	require.Equal(t, uint32(0), e.CompiledModuleCount())
    34  	e.compiledModules[wasm.ModuleID{}] = &compiledModule{}
    35  	require.Equal(t, uint32(1), e.CompiledModuleCount())
    36  }
    37  
    38  func TestEngine_DeleteCompiledModule(t *testing.T) {
    39  	e, ok := NewEngine(ctx, api.CoreFeaturesV1, nil).(*engine)
    40  	require.True(t, ok)
    41  	id := wasm.ModuleID{0xaa}
    42  	err := e.addCompiledModule(&wasm.Module{ID: id}, &compiledModule{
    43  		executables: &executables{executable: make([]byte, 1)},
    44  	})
    45  	require.NoError(t, err)
    46  	require.Equal(t, uint32(1), e.CompiledModuleCount())
    47  	e.DeleteCompiledModule(&wasm.Module{ID: id})
    48  	require.Equal(t, uint32(0), e.CompiledModuleCount())
    49  }
    50  
    51  func Test_ExecutionContextOffsets(t *testing.T) {
    52  	var execCtx executionContext
    53  	require.Equal(t, wazevoapi.Offset(unsafe.Offsetof(execCtx.exitCode)), wazevoapi.ExecutionContextOffsetExitCodeOffset)
    54  	require.Equal(t, wazevoapi.Offset(unsafe.Offsetof(execCtx.callerModuleContextPtr)), wazevoapi.ExecutionContextOffsetCallerModuleContextPtr)
    55  	require.Equal(t, wazevoapi.Offset(unsafe.Offsetof(execCtx.originalFramePointer)), wazevoapi.ExecutionContextOffsetOriginalFramePointer)
    56  	require.Equal(t, wazevoapi.Offset(unsafe.Offsetof(execCtx.originalStackPointer)), wazevoapi.ExecutionContextOffsetOriginalStackPointer)
    57  	require.Equal(t, wazevoapi.Offset(unsafe.Offsetof(execCtx.goReturnAddress)), wazevoapi.ExecutionContextOffsetGoReturnAddress)
    58  	require.Equal(t, wazevoapi.Offset(unsafe.Offsetof(execCtx.goCallReturnAddress)), wazevoapi.ExecutionContextOffsetGoCallReturnAddress)
    59  	require.Equal(t, wazevoapi.Offset(unsafe.Offsetof(execCtx.stackPointerBeforeGoCall)), wazevoapi.ExecutionContextOffsetStackPointerBeforeGoCall)
    60  	require.Equal(t, wazevoapi.Offset(unsafe.Offsetof(execCtx.stackGrowRequiredSize)), wazevoapi.ExecutionContextOffsetStackGrowRequiredSize)
    61  	require.Equal(t, wazevoapi.Offset(unsafe.Offsetof(execCtx.memoryGrowTrampolineAddress)), wazevoapi.ExecutionContextOffsetMemoryGrowTrampolineAddress)
    62  	require.Equal(t, wazevoapi.Offset(unsafe.Offsetof(execCtx.stackGrowCallTrampolineAddress)), wazevoapi.ExecutionContextOffsetStackGrowCallTrampolineAddress)
    63  	require.Equal(t, wazevoapi.Offset(unsafe.Offsetof(execCtx.checkModuleExitCodeTrampolineAddress)), wazevoapi.ExecutionContextOffsetCheckModuleExitCodeTrampolineAddress)
    64  	require.Equal(t, wazevoapi.Offset(unsafe.Offsetof(execCtx.savedRegisters))%16, wazevoapi.Offset(0),
    65  		"SavedRegistersBegin must be aligned to 16 bytes")
    66  	require.Equal(t, wazevoapi.Offset(unsafe.Offsetof(execCtx.savedRegisters)), wazevoapi.ExecutionContextOffsetSavedRegistersBegin)
    67  	require.Equal(t, wazevoapi.Offset(unsafe.Offsetof(execCtx.goFunctionCallCalleeModuleContextOpaque)), wazevoapi.ExecutionContextOffsetGoFunctionCallCalleeModuleContextOpaque)
    68  	require.Equal(t, wazevoapi.Offset(unsafe.Offsetof(execCtx.tableGrowTrampolineAddress)), wazevoapi.ExecutionContextOffsetTableGrowTrampolineAddress)
    69  	require.Equal(t, wazevoapi.Offset(unsafe.Offsetof(execCtx.refFuncTrampolineAddress)), wazevoapi.ExecutionContextOffsetRefFuncTrampolineAddress)
    70  	require.Equal(t, wazevoapi.Offset(unsafe.Offsetof(execCtx.memmoveAddress)), wazevoapi.ExecutionContextOffsetMemmoveAddress)
    71  	require.Equal(t, wazevoapi.Offset(unsafe.Offsetof(execCtx.framePointerBeforeGoCall)), wazevoapi.ExecutionContextOffsetFramePointerBeforeGoCall)
    72  	require.Equal(t, wazevoapi.Offset(unsafe.Offsetof(execCtx.memoryWait32TrampolineAddress)), wazevoapi.ExecutionContextOffsetMemoryWait32TrampolineAddress)
    73  	require.Equal(t, wazevoapi.Offset(unsafe.Offsetof(execCtx.memoryWait64TrampolineAddress)), wazevoapi.ExecutionContextOffsetMemoryWait64TrampolineAddress)
    74  	require.Equal(t, wazevoapi.Offset(unsafe.Offsetof(execCtx.memoryNotifyTrampolineAddress)), wazevoapi.ExecutionContextOffsetMemoryNotifyTrampolineAddress)
    75  }