-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmake_func.go
28 lines (25 loc) · 865 Bytes
/
make_func.go
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
package x64
import (
"fmt"
"reflect"
"unsafe"
)
// Set the executable code for dstAddr. This function is entirely unsafe.
//
// dstAddr must be a pointer to a function value.
// executable must be marked with PROT_EXEC privileges through a MPROTECT system-call.
func SetFunctionCode(dstAddr interface{}, executable []byte) error {
// See "Go 1.1 Function Calls":
// https://docs.google.com/document/d/1bMwCey-gmqZVTpRax-ESeVuZGmjwbocYs1iHplK-cjo/pub
type interfaceHeader struct {
typ uintptr
addr **[]byte
}
v := reflect.ValueOf(dstAddr)
if !v.IsValid() || v.Kind() != reflect.Ptr || v.IsNil() || !v.Elem().CanSet() || v.Elem().Kind() != reflect.Func {
return fmt.Errorf("Destination for SetFunctionCode must be a pointer to a function-value")
}
header := *(*interfaceHeader)(unsafe.Pointer(&dstAddr))
*header.addr = &executable
return nil
}