github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/internal/platform/mmap_test.go (about)

     1  package platform
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/tetratelabs/wazero/internal/testing/require"
     7  )
     8  
     9  func Test_MmapCodeSegment(t *testing.T) {
    10  	if !CompilerSupported() {
    11  		t.Skip()
    12  	}
    13  
    14  	_, err := MmapCodeSegment(1234)
    15  	require.NoError(t, err)
    16  	t.Run("panic on zero length", func(t *testing.T) {
    17  		captured := require.CapturePanic(func() {
    18  			_, _ = MmapCodeSegment(0)
    19  		})
    20  		require.EqualError(t, captured, "BUG: MmapCodeSegment with zero length")
    21  	})
    22  }
    23  
    24  func Test_MunmapCodeSegment(t *testing.T) {
    25  	if !CompilerSupported() {
    26  		t.Skip()
    27  	}
    28  
    29  	// Errors if never mapped
    30  	require.Error(t, MunmapCodeSegment([]byte{1, 2, 3, 5}))
    31  
    32  	newCode, err := MmapCodeSegment(100)
    33  	require.NoError(t, err)
    34  	// First munmap should succeed.
    35  	require.NoError(t, MunmapCodeSegment(newCode))
    36  	// Double munmap should fail.
    37  	require.Error(t, MunmapCodeSegment(newCode))
    38  
    39  	t.Run("panic on zero length", func(t *testing.T) {
    40  		captured := require.CapturePanic(func() {
    41  			_ = MunmapCodeSegment(make([]byte, 0))
    42  		})
    43  		require.EqualError(t, captured, "BUG: MunmapCodeSegment with zero length")
    44  	})
    45  }