wa-lang.org/wazero@v1.0.2/internal/platform/platform.go (about)

     1  // Package platform includes runtime-specific code needed for the compiler or otherwise.
     2  //
     3  // Note: This is a dependency-free alternative to depending on parts of Go's x/sys.
     4  // See /RATIONALE.md for more context.
     5  package platform
     6  
     7  import (
     8  	"errors"
     9  	"io"
    10  	"runtime"
    11  )
    12  
    13  // CompilerSupported is exported for tests and includes constraints here and also the assembler.
    14  func CompilerSupported() bool {
    15  	switch runtime.GOOS {
    16  	case "darwin", "windows", "linux", "freebsd":
    17  	default:
    18  		return false
    19  	}
    20  
    21  	switch runtime.GOARCH {
    22  	case "amd64", "arm64":
    23  	default:
    24  		return false
    25  	}
    26  
    27  	return true
    28  }
    29  
    30  // MmapCodeSegment copies the code into the executable region and returns the byte slice of the region.
    31  //
    32  // See https://man7.org/linux/man-pages/man2/mmap.2.html for mmap API and flags.
    33  func MmapCodeSegment(code io.Reader, size int) ([]byte, error) {
    34  	if size == 0 {
    35  		panic(errors.New("BUG: MmapCodeSegment with zero length"))
    36  	}
    37  	if runtime.GOARCH == "amd64" {
    38  		return mmapCodeSegmentAMD64(code, size)
    39  	} else {
    40  		return mmapCodeSegmentARM64(code, size)
    41  	}
    42  }
    43  
    44  // MunmapCodeSegment unmaps the given memory region.
    45  func MunmapCodeSegment(code []byte) error {
    46  	if len(code) == 0 {
    47  		panic(errors.New("BUG: MunmapCodeSegment with zero length"))
    48  	}
    49  	return munmapCodeSegment(code)
    50  }