google-app-enginepointersgogoogle-app-engine-go

How to fmt.Println() memory not allocated by the go program?


I’d like to print the virtual memory content of the current program from 0x10000 to 0x50000 (an area containing a syscall trampoline on my system).

package main

import (
    "syscall"
    "unsafe"
)
func main() {
    syscall.Syscall(SYS_WRITE, uintptr(1), uintptr(unsafe.Pointer(0x10000)), uintptr(0x40000))
}

However when I tried to compile I’m getting that error :

cannot convert 65536 (type int) to type unsafe.Pointer

In my case,cgo is disabled (import "C" fails at compile time).
Also does syscall.Syscall(SYS_WRITE is the only way to do it ?


Solution

  • You can convert that address to a slice of bytes, which can then be passed to any Write method.

    To convert the address 0x10000 to a slice of bytes with a length of 0x30000, you would use

    mem := (*[1 << 30]byte)(unsafe.Pointer(uintptr(0x10000)))[:0x30000:0x30000]
    

    If the size is always static, you could just set the array to the proper size to start

    mem := (*[0x30000]byte)(unsafe.Pointer(uintptr(0x10000)))[:]