Avoids unnecessary allocations during mmap executables (#1366)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
This commit is contained in:
@@ -2,7 +2,6 @@ package platform
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
@@ -58,7 +57,7 @@ func virtualProtect(address, size, newprotect uintptr, oldprotect *uint32) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func mmapCodeSegmentAMD64(code io.Reader, size int) ([]byte, error) {
|
||||
func mmapCodeSegmentAMD64(size int) ([]byte, error) {
|
||||
p, err := allocateMemory(uintptr(size), windows_PAGE_EXECUTE_READWRITE)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -69,13 +68,10 @@ func mmapCodeSegmentAMD64(code io.Reader, size int) ([]byte, error) {
|
||||
sh.Data = p
|
||||
sh.Len = size
|
||||
sh.Cap = size
|
||||
|
||||
w := &bufWriter{underlying: mem}
|
||||
_, err = io.CopyN(w, code, int64(size))
|
||||
return mem, err
|
||||
}
|
||||
|
||||
func mmapCodeSegmentARM64(code io.Reader, size int) ([]byte, error) {
|
||||
func mmapCodeSegmentARM64(size int) ([]byte, error) {
|
||||
p, err := allocateMemory(uintptr(size), windows_PAGE_READWRITE)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -86,20 +82,16 @@ func mmapCodeSegmentARM64(code io.Reader, size int) ([]byte, error) {
|
||||
sh.Data = p
|
||||
sh.Len = size
|
||||
sh.Cap = size
|
||||
w := &bufWriter{underlying: mem}
|
||||
_, err = io.CopyN(w, code, int64(size))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
old := uint32(windows_PAGE_READWRITE)
|
||||
err = virtualProtect(p, uintptr(size), windows_PAGE_EXECUTE_READ, &old)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return mem, nil
|
||||
}
|
||||
|
||||
var old = uint32(windows_PAGE_READWRITE)
|
||||
|
||||
func MprotectRX(b []byte) (err error) {
|
||||
err = virtualProtect(uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)), windows_PAGE_EXECUTE_READ, &old)
|
||||
return
|
||||
}
|
||||
|
||||
// ensureErr returns syscall.EINVAL when the input error is nil.
|
||||
//
|
||||
// We are supposed to use "GetLastError" which is more precise, but it is not safe to execute in goroutines. While
|
||||
|
||||
Reference in New Issue
Block a user