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

     1  package wazevo
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  	"unsafe"
     7  
     8  	"github.com/tetratelabs/wazero/internal/testing/require"
     9  )
    10  
    11  func TestCallEngine_init(t *testing.T) {
    12  	c := &callEngine{}
    13  	c.init()
    14  	require.True(t, c.stackTop%16 == 0)
    15  	require.Equal(t, &c.stack[0], c.execCtx.stackBottomPtr)
    16  }
    17  
    18  func TestCallEngine_growStack(t *testing.T) {
    19  	t.Run("stack overflow", func(t *testing.T) {
    20  		c := &callEngine{stack: make([]byte, callStackCeiling+1)}
    21  		_, _, err := c.growStack()
    22  		require.Error(t, err)
    23  	})
    24  
    25  	t.Run("ok", func(t *testing.T) {
    26  		s := make([]byte, 32)
    27  		for i := range s {
    28  			s[i] = byte(i)
    29  		}
    30  		c := &callEngine{
    31  			stack:    s,
    32  			stackTop: uintptr(unsafe.Pointer(&s[15])),
    33  			execCtx: executionContext{
    34  				stackGrowRequiredSize:    160,
    35  				stackPointerBeforeGoCall: (*uint64)(unsafe.Pointer(&s[10])),
    36  				framePointerBeforeGoCall: uintptr(unsafe.Pointer(&s[14])),
    37  			},
    38  		}
    39  		newSP, newFp, err := c.growStack()
    40  		require.NoError(t, err)
    41  		require.Equal(t, 160+32*2+16, len(c.stack))
    42  
    43  		require.True(t, c.stackTop%16 == 0)
    44  		require.Equal(t, &c.stack[0], c.execCtx.stackBottomPtr)
    45  
    46  		var view []byte
    47  		{
    48  			sh := (*reflect.SliceHeader)(unsafe.Pointer(&view))
    49  			sh.Data = newSP
    50  			sh.Len = 5
    51  			sh.Cap = 5
    52  		}
    53  		require.Equal(t, []byte{10, 11, 12, 13, 14}, view)
    54  		require.True(t, newSP >= uintptr(unsafe.Pointer(c.execCtx.stackBottomPtr)))
    55  		require.True(t, newSP <= c.stackTop)
    56  		require.Equal(t, newFp-newSP, uintptr(4))
    57  	})
    58  }
    59  
    60  func TestCallEngine_requiredInitialStackSize(t *testing.T) {
    61  	c := &callEngine{}
    62  	require.Equal(t, 10240, c.requiredInitialStackSize())
    63  	c.sizeOfParamResultSlice = 10
    64  	require.Equal(t, 10240, c.requiredInitialStackSize())
    65  	c.sizeOfParamResultSlice = 1000
    66  	require.Equal(t, 1000*16+32+16, c.requiredInitialStackSize())
    67  }