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

     1  package arm64
     2  
     3  import (
     4  	"encoding/binary"
     5  	"encoding/hex"
     6  	"testing"
     7  
     8  	"github.com/tetratelabs/wazero/internal/engine/wazevo/ssa"
     9  	"github.com/tetratelabs/wazero/internal/testing/require"
    10  )
    11  
    12  func Test_maxNumFunctions(t *testing.T) {
    13  	// Ensures that trampoline island is small enough to fit in the interval.
    14  	require.True(t, maxNumFunctions*trampolineCallSize < trampolineIslandInterval/2) //nolint:gomnd
    15  	// Ensures that each function is small enough to fit in the interval.
    16  	require.True(t, maxFunctionExecutableSize < trampolineIslandInterval/2) //nolint:gomnd
    17  }
    18  
    19  func Test_encodeCallTrampolineIsland(t *testing.T) {
    20  	executable := make([]byte, 16*1000)
    21  	islandOffset := 160
    22  	refToBinaryOffset := []int{0: 0, 1: 16, 2: 1600, 3: 16000}
    23  	encodeCallTrampolineIsland(refToBinaryOffset, islandOffset, executable)
    24  	for i := 0; i < len(refToBinaryOffset); i++ {
    25  		offset := islandOffset + trampolineCallSize*i
    26  		instrs := executable[offset : offset+trampolineCallSize-4]
    27  		// Instructions are always the same except for the last immediate.
    28  		require.Equal(t, "9b0000106b0380b97b030b8b60031fd6", hex.EncodeToString(instrs))
    29  		imm := binary.LittleEndian.Uint32(executable[offset+trampolineCallSize-4:])
    30  		require.Equal(t, uint32(refToBinaryOffset[ssa.FuncRef(i)]-(offset+16)), imm)
    31  	}
    32  }
    33  
    34  func Test_searchTrampolineIsland(t *testing.T) {
    35  	offsets := []int{16, 32, 48}
    36  	require.Equal(t, 32, searchTrampolineIsland(offsets, 30))
    37  	require.Equal(t, 32, searchTrampolineIsland(offsets, 17))
    38  	require.Equal(t, 16, searchTrampolineIsland(offsets, 16))
    39  	require.Equal(t, 48, searchTrampolineIsland(offsets, 48))
    40  	require.Equal(t, 48, searchTrampolineIsland(offsets, 56))
    41  	require.Equal(t, 16, searchTrampolineIsland(offsets, 1))
    42  }