github.com/notti/go-dynamic@v0.0.0-20190619201224-fc443047424c/steps/2_go/prog/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"runtime"
     7  	"unsafe"
     8  )
     9  
    10  var puts__dynload uintptr
    11  var strcat__dynload uintptr
    12  
    13  //go:linkname asmcgocall runtime.asmcgocall
    14  func asmcgocall(unsafe.Pointer, uintptr) int32
    15  
    16  //go:linkname entersyscall runtime.entersyscall
    17  func entersyscall()
    18  
    19  //go:linkname exitsyscall runtime.exitsyscall
    20  func exitsyscall()
    21  
    22  func asmcall3()
    23  
    24  func call3(fn uintptr, arg0 uintptr, arg1 uintptr, arg2 uintptr) uintptr {
    25  	p := unsafe.Pointer(reflect.ValueOf(asmcall3).Pointer())
    26  
    27  	entersyscall()
    28  	asmcgocall(p, uintptr(unsafe.Pointer(&fn)))
    29  	exitsyscall()
    30  
    31  	runtime.KeepAlive(p)
    32  	runtime.KeepAlive(fn)
    33  	return fn
    34  }
    35  
    36  var dings uintptr
    37  
    38  func main() {
    39  	str := "hello world"
    40  	b := append([]byte(str), 0)
    41  
    42  	fmt.Println(call3(puts__dynload, uintptr(unsafe.Pointer(&b[0])), 0, 0))
    43  
    44  	runtime.KeepAlive(b)
    45  
    46  	teststr := []byte("hello\000world\000")
    47  	teststr2 := []byte("C!\000")
    48  	fmt.Println(call3(strcat__dynload, uintptr(unsafe.Pointer(&teststr[0])), uintptr(unsafe.Pointer(&teststr2[0])), 0))
    49  	runtime.KeepAlive(teststr2)
    50  
    51  	fmt.Println(call3(puts__dynload, uintptr(unsafe.Pointer(&teststr[0])), 0, 0))
    52  	runtime.KeepAlive(teststr)
    53  
    54  	fmt.Println(teststr)
    55  }