Skip to content

Instantly share code, notes, and snippets.

@Perelandric
Last active November 11, 2016 23:50
Show Gist options
  • Save Perelandric/133e3e1fa3d35cc9d1d371f3ef1627f1 to your computer and use it in GitHub Desktop.
Save Perelandric/133e3e1fa3d35cc9d1d371f3ef1627f1 to your computer and use it in GitHub Desktop.
Statically link to pony library from Go project.
package main
/*
#cgo CFLAGS: -I${SRCDIR}/lib
#cgo CFLAGS: -I$PONYHOME/src/libponyrt/
#cgo LDFLAGS: ${SRCDIR}/lib/libtest.a
#cgo LDFLAGS: -L$PONYHOME/build/release -lponyrt
#cgo LDFLAGS: -lpthread
#cgo LDFLAGS: -ldl
#include "pony.h"
#include "lib/test.h"
*/
import "C"
import (
"time"
"unsafe"
)
func main() {
// Initialize
const argc = 1
var argv = [argc]*C.char{C.CString("test")}
C.pony_init(argc, unsafe.Pointer(&argv))
// Create actor and arg
var t = C.Test_Alloc()
var str = C.CString("test")
// Call constructor with string
C.Test_tag_create_oo__send(t, str)
C.pony_start(true)
// Wait a second
time.Sleep(time.Second)
}
actor @Test
new create(s:Pointer[U8] tag) =>
None
@Perelandric
Copy link
Author

The C.CString() produces a *C.char, which is what the generated C function receives, but it segfaults.

There's no problem if I eliminate the string argument or change it to another type, like bool.

> go run test.go
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0xa8 pc=0x4604e0]

runtime stack:
runtime.throw(0x481a25, 0x2a)
	/usr/local/go/src/runtime/panic.go:566 +0x95
runtime.sigpanic()
	/usr/local/go/src/runtime/sigpanic_unix.go:12 +0x2cc

goroutine 1 [syscall, locked to thread]:
runtime.cgocall(0x457380, 0xc420055ed0, 0x0)
	/usr/local/go/src/runtime/cgocall.go:131 +0x110 fp=0xc420055ea0 sp=0xc420055e60
main._Cfunc_Test_tag_create_oo__send(0x7fcc337fd800, 0x2567c80, 0x0)
	command-line-arguments/_obj/_cgo_gotypes.go:76 +0x4e fp=0xc420055ed0 sp=0xc420055ea0
main.main()
	/home/mainuser/Desktop/test/test.go:31 +0x1d0 fp=0xc420055f48 sp=0xc420055ed0
runtime.main()
	/usr/local/go/src/runtime/proc.go:183 +0x1f4 fp=0xc420055fa0 sp=0xc420055f48
runtime.goexit()
	/usr/local/go/src/runtime/asm_amd64.s:2086 +0x1 fp=0xc420055fa8 sp=0xc420055fa0

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
	/usr/local/go/src/runtime/asm_amd64.s:2086 +0x1
exit status 2

@Perelandric
Copy link
Author

Some gdb info:

(gdb) run
Starting program: .../test/test 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff73dc700 (LWP 7723)]
[New Thread 0x7ffff6bdb700 (LWP 7724)]
[New Thread 0x7ffff5bd9700 (LWP 7726)]
[New Thread 0x7ffff63da700 (LWP 7725)]

Program received signal SIGSEGV, Segmentation fault.
0x00000000004604e0 in ponyint_gc_done ()
(gdb) bt
#0  0x00000000004604e0 in ponyint_gc_done ()
#1  0x00000000004574fe in Test_tag_create_oo__send ()
#2  0x000000000045739c in _cgo_e3a49bdcc536_Cfunc_Test_tag_create_oo__send (v=0xc420055ed0) at .../test/test.go:72
#3  0x00000000004533d0 in runtime.asmcgocall () at /usr/local/go/src/runtime/asm_amd64.s:590
#4  0x00000000004082bd in runtime.cgocall (fn=0x7fffffffd9e0, arg=0x6b7dc0 , ~r2=-9776)
    at /usr/local/go/src/runtime/cgocall.go:115
#5  0x00000000006b7e00 in runtime.g0 ()
#6  0x00007fffffffd9e0 in ?? ()
#7  0x00000000006b7dc0 in ?? ()
#8  0x00007fffffffd9d0 in ?? ()
#9  0x000000000042ea14 in runtime.mstart () at /usr/local/go/src/runtime/proc.go:1096
#10 0x0000000000451535 in runtime.rt0_go () at /usr/local/go/src/runtime/asm_amd64.s:156
#11 0x0000000000000001 in ?? ()
#12 0x00007fffffffdae8 in ?? ()
#13 0x0000000000000001 in ?? ()
#14 0x00007fffffffdae8 in ?? ()
#15 0x0000000000000000 in ?? ()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment