2026-08-21 00:03:30 +02:00
|
|
|
// Copyright (c) 2026 Petr Balvín <opensource@petrbalvin.org> (https://petrbalvin.org)
|
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
|
|
|
|
//go:build arm64
|
|
|
|
|
|
|
|
|
|
package verify
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/binary"
|
|
|
|
|
"fmt"
|
|
|
|
|
"syscall"
|
|
|
|
|
"unsafe"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// enterJIT switches to the prepared stack and branches to fn.
|
|
|
|
|
//
|
|
|
|
|
//go:nosplit
|
|
|
|
|
func enterJIT(fn uintptr, stack uintptr)
|
|
|
|
|
|
|
|
|
|
// leaveJIT restores the Go stack after a JIT function returns.
|
|
|
|
|
//
|
|
|
|
|
//go:nosplit
|
|
|
|
|
func leaveJIT()
|
|
|
|
|
|
2026-08-30 22:27:20 +02:00
|
|
|
// leaveJITAddr is the raw ABI0 address of leaveJIT, handed over by the
|
|
|
|
|
// GLOBL/DATA in trampoline_arm64.s (reflect would return the interposed
|
|
|
|
|
// ABIInternal wrapper instead).
|
|
|
|
|
var leaveRawAddr uintptr
|
2026-08-21 00:03:30 +02:00
|
|
|
|
2026-08-30 22:27:20 +02:00
|
|
|
var leaveJITAddr = leaveRawAddr
|
2026-08-21 00:03:30 +02:00
|
|
|
|
|
|
|
|
const stackPad = 64
|
|
|
|
|
|
|
|
|
|
// Call invokes the assembled function at fnAddr with the given ABI0 argument
|
|
|
|
|
// block. On arm64, the ABI0 convention places arguments on the stack starting
|
|
|
|
|
// at [SP+8] (same as amd64 ABI0 in this project).
|
2026-09-19 23:49:19 +02:00
|
|
|
//
|
|
|
|
|
// Not safe for concurrent use: only one JIT call may be in flight at a
|
|
|
|
|
// time, the trampolines keep the saved registers in package globals.
|
2026-08-21 00:03:30 +02:00
|
|
|
func Call(fnAddr uintptr, args []byte) ([]byte, error) {
|
|
|
|
|
stackSize := stackPad + 8 + len(args) + 64
|
|
|
|
|
stackMem, err := syscall.Mmap(-1, 0, stackSize,
|
|
|
|
|
syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_PRIVATE|syscall.MAP_ANON)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("verify: stack mmap: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer syscall.Munmap(stackMem)
|
|
|
|
|
|
|
|
|
|
retOff := stackPad
|
|
|
|
|
binary.LittleEndian.PutUint64(stackMem[retOff:retOff+8], uint64(leaveJITAddr))
|
|
|
|
|
copy(stackMem[retOff+8:], args)
|
|
|
|
|
|
|
|
|
|
stackBase := uintptr(unsafe.Pointer(&stackMem[retOff]))
|
|
|
|
|
enterJIT(fnAddr, stackBase)
|
|
|
|
|
|
|
|
|
|
out := make([]byte, len(args))
|
|
|
|
|
copy(out, stackMem[retOff+8:retOff+8+len(args)])
|
|
|
|
|
return out, nil
|
|
|
|
|
}
|