github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/internal/engine/wazevo/call_engine_test.go (about) 1 package wazevo 2 3 import ( 4 "reflect" 5 "testing" 6 "unsafe" 7 8 "github.com/wasilibs/wazerox/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 }, 37 } 38 newSP, err := c.growStack() 39 require.NoError(t, err) 40 require.Equal(t, 160+32*2+16, len(c.stack)) 41 42 require.True(t, c.stackTop%16 == 0) 43 require.Equal(t, &c.stack[0], c.execCtx.stackBottomPtr) 44 45 var view []byte 46 { 47 sh := (*reflect.SliceHeader)(unsafe.Pointer(&view)) 48 sh.Data = newSP 49 sh.Len = 5 50 sh.Cap = 5 51 } 52 require.Equal(t, []byte{10, 11, 12, 13, 14}, view) 53 require.True(t, newSP >= uintptr(unsafe.Pointer(c.execCtx.stackBottomPtr))) 54 require.True(t, newSP <= c.stackTop) 55 }) 56 } 57 58 func TestCallEngine_requiredInitialStackSize(t *testing.T) { 59 c := &callEngine{} 60 require.Equal(t, 512, c.requiredInitialStackSize()) 61 c.sizeOfParamResultSlice = 10 62 require.Equal(t, 512, c.requiredInitialStackSize()) 63 c.sizeOfParamResultSlice = 120 64 require.Equal(t, 120*16+32+16, c.requiredInitialStackSize()) 65 }