Files

187 lines
5.1 KiB
Go

// Copyright (C) 2026 ciciplusplus
// SPDX-License-Identifier: GPL-2.0-only
package main
import (
"encoding/binary"
"unsafe"
uc "github.com/unicorn-engine/unicorn/bindings/go/unicorn"
)
const (
mallocBaseAddress = 0x80000000
)
type Memory struct {
allocator *Allocator
// TODO: get rid of allocatedSizes
allocatedSizes map[uint32]uint32
hostToGuestPointers map[uint64]uint32
guestToHostPointers map[uint32]uint64
}
func NewMemory(mu uc.Unicorn, size int) *Memory {
allocator := NewAllocator(size)
start := allocator.GetStartAddress()
hostStartPtr := uintptr(start)
logInfo("mem start 0x%x, mem size %d\n", hostStartPtr, size)
// TODO: mem protections
if err := mu.MemMapPtr(mallocBaseAddress, uint64(size), uc.PROT_ALL, unsafe.Pointer(hostStartPtr)); err != nil {
logPanic("Unable to map malloc memory to Unicorn: %w", err)
}
return &Memory{
allocator: allocator,
allocatedSizes: make(map[uint32]uint32),
hostToGuestPointers: make(map[uint64]uint32),
guestToHostPointers: make(map[uint32]uint64),
}
}
func (mem *Memory) Malloc(size uint32) ([]byte, uint32) {
if size == 0 {
size = 1
}
allocd, err := mem.allocator.Malloc(int(size))
if err != nil {
panic(err)
}
hostStartPtr, hostSize := mem.HostRegion()
if !mem.SliceInHostRegion(allocd, hostStartPtr, hostSize) {
logPanic("FATAL allocation of size %d is outside expected host range!\n", size)
}
res := mem.SliceToGuestAddr(allocd)
mem.allocatedSizes[res] = size
return allocd, res
}
func (mem *Memory) Realloc(ptr uint32, newSize uint32) uint32 {
logInfo("realloc(0x%x, %d)\n", ptr, newSize)
if ptr == 0 {
// realloc(NULL, size) -> malloc(size)
_, res := mem.Malloc(newSize)
return res
}
size, ok := mem.allocatedSizes[ptr]
if !ok {
logPanic("Unknown guest allocation 0x%x for _realloc", ptr)
}
if newSize <= size {
return ptr
} else {
allocd, res := mem.Malloc(newSize)
oldAllocd := mem.GuestAddrToSlice(ptr, size)
copy(allocd, oldAllocd)
mem.Free(ptr)
return res
}
}
func (mem *Memory) Free(addr uint32) {
size, ok := mem.allocatedSizes[addr]
if !ok {
logPanic("Unknown guest allocation 0x%x for free", addr)
}
delete(mem.allocatedSizes, addr)
slice := mem.GuestAddrToSlice(addr, size)
mem.allocator.Free(slice)
}
func (mem *Memory) HostRegion() (uintptr, uintptr) {
return uintptr(mem.allocator.GetStartAddress()), uintptr(mem.allocator.GetSize())
}
func (mem *Memory) SliceInHostRegion(slice []byte, hostStartPtr uintptr, hostSize uintptr) bool {
if len(slice) == 0 {
return false
}
slicePtr := uintptr(unsafe.Pointer(&slice[0]))
return slicePtr >= hostStartPtr && slicePtr+uintptr(len(slice)) <= hostStartPtr+hostSize
}
func (mem *Memory) SliceToGuestAddr(slice []byte) uint32 {
hostStartPtr, hostSize := mem.HostRegion()
slicePtr := uintptr(unsafe.Pointer(&slice[0]))
if slicePtr < hostStartPtr || slicePtr+uintptr(len(slice)) > hostStartPtr+hostSize {
logPanic("Allocation [0x%x, 0x%x) is outside of the guest heap [0x%x, 0x%x)!",
slicePtr, slicePtr+uintptr(len(slice)), hostStartPtr, hostStartPtr+hostSize)
}
guestAddr := mallocBaseAddress + slicePtr - hostStartPtr
return uint32(guestAddr)
}
func (mem *Memory) GuestAddrToSlice(guestAddr uint32, length uint32) []byte {
hostStartPtr, hostSize := mem.HostRegion()
if uintptr(guestAddr) < mallocBaseAddress ||
uintptr(guestAddr)+uintptr(length) > mallocBaseAddress+hostSize {
logPanic("Guest heap range [0x%x, 0x%x) is outside of the guest heap [0x%x, 0x%x)!",
guestAddr, uintptr(guestAddr)+uintptr(length), uintptr(mallocBaseAddress), mallocBaseAddress+hostSize)
}
hostAddr := uintptr(guestAddr) + hostStartPtr - mallocBaseAddress
tmp := (*byte)(unsafe.Pointer(hostAddr))
return unsafe.Slice(tmp, length)
}
func (mem *Memory) GuestAddrToHostPointer(guestAddr uint32) unsafe.Pointer {
slice := mem.GuestAddrToSlice(guestAddr, 1)
return unsafe.Pointer(&slice[0])
}
func (mem *Memory) GetHostPointer(guestAddr uint32) uint64 {
hostAddr, ok := mem.guestToHostPointers[guestAddr]
if !ok {
logPanic("Unknown guest pointer 0x%x to find host pointer!", guestAddr)
}
return hostAddr
}
func (mem *Memory) GetGuestOrRegisterHostPointer(hostAddr uint64) uint32 {
var guestAddr uint32
var ok bool
guestAddr, ok = mem.hostToGuestPointers[hostAddr]
if !ok {
guestAddr = mem.RegisterHostPointer(hostAddr)
}
return guestAddr
}
func (mem *Memory) RegisterHostPointer(hostAddr uint64) uint32 {
var guestAddr uint32
var allocd []byte
allocd, guestAddr = mem.Malloc(8)
binary.LittleEndian.PutUint64(allocd, hostAddr)
mem.hostToGuestPointers[hostAddr] = guestAddr
mem.guestToHostPointers[guestAddr] = hostAddr
return guestAddr
}
func (mem *Memory) UnregisterHostPointer(hostAddr uint64) {
guestAddr, ok := mem.hostToGuestPointers[hostAddr]
if !ok {
logPanic("Unknown host pointer 0x%x to find guest pointer!", hostAddr)
}
tmp := mem.GuestAddrToSlice(guestAddr, 8)
storedHostAddr := binary.LittleEndian.Uint64(tmp)
if storedHostAddr != hostAddr {
logPanic("Unknown stored host pointer 0x%x != passed one 0x%x!", storedHostAddr, hostAddr)
}
mem.Free(guestAddr)
delete(mem.hostToGuestPointers, hostAddr)
delete(mem.guestToHostPointers, guestAddr)
}