platform.go

 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.
 5package platform
 6
 7import (
 8	"runtime"
 9
10	"github.com/tetratelabs/wazero/api"
11	"github.com/tetratelabs/wazero/experimental"
12)
13
14// CompilerSupported includes constraints here and also the assembler.
15func CompilerSupported() bool {
16	return CompilerSupports(api.CoreFeaturesV2)
17}
18
19func CompilerSupports(features api.CoreFeatures) bool {
20	switch runtime.GOOS {
21	case "linux", "darwin", "freebsd", "netbsd", "dragonfly", "windows":
22		if runtime.GOARCH == "arm64" {
23			if features.IsEnabled(experimental.CoreFeaturesThreads) {
24				return CpuFeatures.Has(CpuFeatureArm64Atomic)
25			}
26			return true
27		}
28		fallthrough
29	case "solaris", "illumos":
30		return runtime.GOARCH == "amd64" && CpuFeatures.Has(CpuFeatureAmd64SSE4_1)
31	default:
32		return false
33	}
34}
35
36// MmapCodeSegment copies the code into the executable region and returns the byte slice of the region.
37//
38// See https://man7.org/linux/man-pages/man2/mmap.2.html for mmap API and flags.
39func MmapCodeSegment(size int) ([]byte, error) {
40	if size == 0 {
41		panic("BUG: MmapCodeSegment with zero length")
42	}
43	if runtime.GOARCH == "amd64" {
44		return mmapCodeSegmentAMD64(size)
45	} else {
46		return mmapCodeSegmentARM64(size)
47	}
48}
49
50// MunmapCodeSegment unmaps the given memory region.
51func MunmapCodeSegment(code []byte) error {
52	if len(code) == 0 {
53		panic("BUG: MunmapCodeSegment with zero length")
54	}
55	return munmapCodeSegment(code)
56}