Files
wazero/internal/platform/platform.go
Crypt Keeper 50d504cfdd Narrows compiler support to operating systems currently known to work (#612)
This narrows to what the `internal/platform` package supports, which is
currently bound by Go SDK source that include `Mprotect` or windows.

Ex. `zsyscall_linux_amd64.go` includes `Mprotect`, but
`zsyscall_freebsd_amd64.go` does not.

This should prevent errors like below, by allowing `wazero.NewRuntime()`
to properly fallback to the interpreter.

```
.../mmap.go:74:16: undefined: syscall.Mprotect
```

A later change will implement FreeBSD. This is just here to ensure users
don't crash on unexpected OS.

See #607

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-06-01 13:49:55 +08:00

50 lines
1.2 KiB
Go

// Package platform includes runtime-specific code needed for the compiler or otherwise.
//
// Note: This is a dependency-free alternative to depending on parts of Go's x/sys.
// See /RATIONALE.md for more context.
package platform
import (
"errors"
"runtime"
)
// IsSupported is exported for tests and includes constraints here and also the assembler.
func IsSupported() bool {
switch runtime.GOOS {
case "darwin", "windows", "linux":
default:
return false
}
switch runtime.GOARCH {
case "amd64", "arm64":
default:
return false
}
return true
}
// MmapCodeSegment copies the code into the executable region and returns the byte slice of the region.
//
// See https://man7.org/linux/man-pages/man2/mmap.2.html for mmap API and flags.
func MmapCodeSegment(code []byte) ([]byte, error) {
if len(code) == 0 {
panic(errors.New("BUG: MmapCodeSegment with zero length"))
}
if runtime.GOARCH == "amd64" {
return mmapCodeSegmentAMD64(code)
} else {
return mmapCodeSegmentARM64(code)
}
}
// MunmapCodeSegment unmaps the given memory region.
func MunmapCodeSegment(code []byte) error {
if len(code) == 0 {
panic(errors.New("BUG: MunmapCodeSegment with zero length"))
}
return munmapCodeSegment(code)
}