Files
wazero/internal/platform/mmap.go
Crypt Keeper 891761ac1e Adds support for FreeBSD (#671)
FreeBSD was disabled due to lack of testing. This works around the
compilation problems. Note: We can't currently test arm64 automatically!

Notes:

* GitHub Actions doesn’t support FreeBSD, and may never.
* We could use Travis to run FreeBSD, but it would split our CI config.
* Using Vagrant directly is easier to debug than vmactions/freebsd-vm.
* GitHub Actions only supports virtualization on MacOS.
* GitHub Actions removed vagrant from the image starting with macos-11.
* Since VirtualBox doesn't work on arm64, freebsd/arm64 is untestabl

Signed-off-by: Adrian Cole <adrian@tetrate.io>
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
Co-authored-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-07-01 18:59:19 +08:00

77 lines
2.1 KiB
Go

// This uses syscall.Mprotect. Go's SDK only supports this on darwin and linux.
//go:build darwin || linux || freebsd
package platform
import (
"syscall"
"unsafe"
)
func munmapCodeSegment(code []byte) error {
return syscall.Munmap(code)
}
// mmapCodeSegmentAMD64 gives all read-write-exec permission to the mmap region
// to enter the function. Otherwise, segmentation fault exception is raised.
func mmapCodeSegmentAMD64(code []byte) ([]byte, error) {
mmapFunc, err := syscall.Mmap(
-1,
0,
len(code),
// The region must be RWX: RW for writing native codes, X for executing the region.
syscall.PROT_READ|syscall.PROT_WRITE|syscall.PROT_EXEC,
// Anonymous as this is not an actual file, but a memory,
// Private as this is in-process memory region.
syscall.MAP_ANON|syscall.MAP_PRIVATE,
)
if err != nil {
return nil, err
}
copy(mmapFunc, code)
return mmapFunc, nil
}
// mmapCodeSegmentARM64 cannot give all read-write-exec permission to the mmap region.
// Otherwise, the mmap systemcall would raise an error. Here we give read-write
// to the region at first, write the native code and then change the perm to
// read-exec so we can execute the native code.
func mmapCodeSegmentARM64(code []byte) ([]byte, error) {
mmapFunc, err := syscall.Mmap(
-1,
0,
len(code),
// The region must be RW: RW for writing native codes.
syscall.PROT_READ|syscall.PROT_WRITE,
// Anonymous as this is not an actual file, but a memory,
// Private as this is in-process memory region.
syscall.MAP_ANON|syscall.MAP_PRIVATE,
)
if err != nil {
return nil, err
}
copy(mmapFunc, code)
// Then we're done with writing code, change the permission to RX.
err = mprotect(mmapFunc, syscall.PROT_READ|syscall.PROT_EXEC)
return mmapFunc, err
}
var _zero uintptr
// mprotect is like syscall.Mprotect, defined locally so that freebsd compiles.
func mprotect(b []byte, prot int) (err error) {
var _p0 unsafe.Pointer
if len(b) > 0 {
_p0 = unsafe.Pointer(&b[0])
} else {
_p0 = unsafe.Pointer(&_zero)
}
_, _, e1 := syscall.Syscall(syscall.SYS_MPROTECT, uintptr(_p0), uintptr(len(b)), uintptr(prot))
if e1 != 0 {
err = syscall.Errno(e1)
}
return
}