blob: 8d0baa712cf0733bb975633b4fb33cb2de7443f3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
//go:build (linux || darwin || freebsd || netbsd || dragonfly || solaris) && !tinygo
package platform
import (
"syscall"
)
const (
mmapProtAMD64 = syscall.PROT_READ | syscall.PROT_WRITE | syscall.PROT_EXEC
mmapProtARM64 = syscall.PROT_READ | syscall.PROT_WRITE
)
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(size int) ([]byte, error) {
// The region must be RWX: RW for writing native codes, X for executing the region.
return mmapCodeSegment(size, mmapProtAMD64)
}
// 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 so that we can write contents at call-sites. Callers are responsible to
// execute MprotectRX on the returned buffer.
func mmapCodeSegmentARM64(size int) ([]byte, error) {
// The region must be RW: RW for writing native codes.
return mmapCodeSegment(size, mmapProtARM64)
}
|