cgocall unavailable without CGO_ENABLED=0
iamacarpet opened this issue · 6 comments
Hello @notti
Just finishing up my documentation and I noticed in yours it says:
WARNING nocgo supports both cgo and missing cgo as environment. So if you want to ensure cgo not being used don't forget CGO_ENABLED=0 as environment variable to go build.
However, building without CGO_ENABLED=0
always throws me this error:
fatal error: cgocall unavailable
goroutine 1 [running, locked to thread]:
runtime.throw(0x4f53b4, 0x13)
/usr/local/go/src/runtime/panic.go:617 +0x72 fp=0xc000090d50 sp=0xc000090d20 pc=0x429d02
runtime.cgocall(0x4a8ed0, 0xc000090d98, 0xc000090db8)
/usr/local/go/src/runtime/cgocall.go:96 +0xe4 fp=0xc000090d88 sp=0xc000090d50 pc=0x403d34
github.com/notti/nocgo.callWrapper(0xc000086010, 0x10, 0x10, 0xc000000002, 0xf, 0x10, 0x7fbff35f9d98, 0x0, 0xaa, 0xc000090f38, ...)
~/go-projects/src/github.com/notti/nocgo/call_amd64.s:69 +0x49 fp=0xc000090db8 sp=0xc000090d88 pc=0x4a8ea9
github.com/notti/nocgo.Open(0x4f47d3, 0xf, 0x40b768, 0x70, 0x4e6f60)
~/go-projects/src/github.com/notti/nocgo/dlopen.go:61 +0x84 fp=0xc000090e10 sp=0xc000090db8 pc=0x4a7f54
github.com/iamacarpet/go-sqlite3-dynamic.registerLibrary()
~/go-projects/src/github.com/iamacarpet/go-sqlite3-dynamic/dynamic_register_linux.go:65 +0x4b fp=0xc000090f48 sp=0xc000090e10 pc=0x4ac4cb
github.com/iamacarpet/go-sqlite3-dynamic.init.0()
~/go-projects/src/github.com/iamacarpet/go-sqlite3-dynamic/sqlite3.go:23 +0x22 fp=0xc000090f78 sp=0xc000090f48 pc=0x4ad9e2
github.com/iamacarpet/go-sqlite3-dynamic.init()
<autogenerated>:1 +0x93 fp=0xc000090f88 sp=0xc000090f78 pc=0x4b3a33
main.init()
<autogenerated>:1 +0x54 fp=0xc000090f98 sp=0xc000090f88 pc=0x4b4654
runtime.main()
/usr/local/go/src/runtime/proc.go:188 +0x1c8 fp=0xc000090fe0 sp=0xc000090f98 pc=0x42b628
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1337 +0x1 fp=0xc000090fe8 sp=0xc000090fe0 pc=0x454c71
Is this expected behavior?
Regards,
iamacarpet
Ah you managed to hit an untested path. So far I only tested with something that uses network functions which includes cgo if not explicitly asked not to do that...
I'll try to fix that if possible
Hrmpf I investigated a lot only to discover, that I misread your bugreport. CGO_ENABLED=1 doesn't work here if there is no cgo used in the rest of the program...
CGO_ENABLED=0 works as expected here.
@iamacarpet How did you reach the above error?
Sorry for being unclear @notti
Had a test script for my go-sqlite-dynamic repo, compiled it with just ‘go build’ and at runtime it threw that error, however ‘CGO_ENABLE=0 go build’ resulted on a binary that ran fine.
I thought you’d hit the nail on the head by saying you’d only tested in apps where real CGO was used elsewhere (i.e. including real C dependancies), where as my app reported being dynamically linked from ‘ldd’ but included nothing calling CGO.
Does it default to using the system linker if it is installed and you don’t specify ‘CGO_ENABLED=0’ maybe?
On mobile at the moment so will try to provide more clear details tomorrow.
Hopefully this will help to clarify, the code:
package main
import (
"os"
"fmt"
"database/sql"
"github.com/iamacarpet/go-sqlite3-dynamic"
)
func main() {
path := "test.db"
f, err := os.Create(path)
if err != nil {
panic(err)
}
f.Close()
fmt.Println(sqlite3.Version())
db, err := sql.Open(`sqlite3`, path)
if err != nil {
panic(err)
}
r, err := db.Exec(`CREATE TABLE test (
id integer PRIMARY KEY NOT NULL,
name varchar(30)
)`)
if err != nil {
panic(err)
}
_ = r
r, err = db.Exec(`INSERT INTO test(name) VALUES ('first') `)
if err != nil {
panic(err)
}
_, err = r.LastInsertId()
if err != nil {
panic(err)
}
_, err = r.RowsAffected()
if err != nil {
panic(err)
}
r, err = db.Exec(`INSERT INTO test(name) VALUES ('second') `)
if err != nil {
panic(err)
}
_, err = r.LastInsertId()
if err != nil {
panic(err)
}
_, err = r.RowsAffected()
if err != nil {
panic(err)
}
db.Close()
os.Remove(`./test.db`)
}
Any my Go version:
$ go version
go version go1.12.4 linux/amd64
The results:
- Without specifying anything
$ go build -o test-1
$ ./test-1
fatal error: cgocall unavailable
goroutine 1 [running, locked to thread]:
runtime.throw(0x4f5664, 0x13)
/usr/local/go/src/runtime/panic.go:617 +0x72 fp=0xc000030550 sp=0xc000030520 pc=0x429e72
runtime.cgocall(0x4a9820, 0xc000030598, 0xc0000305b8)
/usr/local/go/src/runtime/cgocall.go:96 +0xe4 fp=0xc000030588 sp=0xc000030550 pc=0x403ea4
github.com/notti/nocgo.callWrapper(0xc000016090, 0x10, 0x10, 0xc000000002, 0xf, 0x10, 0x7f73bcd2d008, 0x0, 0x30, 0xc000030738, ...)
~/go-projects/src/github.com/notti/nocgo/call_amd64.s:69 +0x49 fp=0xc0000305b8 sp=0xc000030588 pc=0x4a97f9
github.com/notti/nocgo.Open(0x4f4a83, 0xf, 0x40b8d8, 0x70, 0x4e71e0)
~/go-projects/src/github.com/notti/nocgo/dlopen.go:61 +0x84 fp=0xc000030610 sp=0xc0000305b8 pc=0x4a88a4
github.com/iamacarpet/go-sqlite3-dynamic.registerLibrary()
~/go-projects/src/github.com/iamacarpet/go-sqlite3-dynamic/dynamic_register_linux.go:65 +0x4b fp=0xc000030748 sp=0xc000030610 pc=0x4ace1b
github.com/iamacarpet/go-sqlite3-dynamic.init.0()
~/go-projects/src/github.com/iamacarpet/go-sqlite3-dynamic/sqlite3.go:23 +0x22 fp=0xc000030778 sp=0xc000030748 pc=0x4ae332
github.com/iamacarpet/go-sqlite3-dynamic.init()
<autogenerated>:1 +0x93 fp=0xc000030788 sp=0xc000030778 pc=0x4b4383
main.init()
<autogenerated>:1 +0x54 fp=0xc000030798 sp=0xc000030788 pc=0x4b4f84
runtime.main()
/usr/local/go/src/runtime/proc.go:188 +0x1c8 fp=0xc0000307e0 sp=0xc000030798 pc=0x42b798
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1337 +0x1 fp=0xc0000307e8 sp=0xc0000307e0 pc=0x454de1
$ ldd ./test-1
linux-vdso.so.1 (0x00007fffbfccd000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fe205b16000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fe205725000)
/lib64/ld-linux-x86-64.so.2 (0x00007fe205d1a000)
- Specifying
CGO_ENABLED=0
$ CGO_ENABLED=0 go build -o test-2
$ ./test-2
Unknown 3022000 2018-01-22 18:45:57 0c55d179733b46d8d0ba4d88e01a25e10677046ee3da1d5b1581e86726f2alt1
$ ldd ./test-2
linux-vdso.so.1 (0x00007ffe08545000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ffb6fd7e000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ffb6fb5f000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ffb6f76e000)
/lib64/ld-linux-x86-64.so.2 (0x00007ffb6ff82000)
- Specifying
CGO_ENABLED=1
$ CGO_ENABLED=1 go build -o test-3
$ ./test-3
fatal error: cgocall unavailable
goroutine 1 [running, locked to thread]:
runtime.throw(0x4f5664, 0x13)
/usr/local/go/src/runtime/panic.go:617 +0x72 fp=0xc000030550 sp=0xc000030520 pc=0x429e72
runtime.cgocall(0x4a9820, 0xc000030598, 0xc0000305b8)
/usr/local/go/src/runtime/cgocall.go:96 +0xe4 fp=0xc000030588 sp=0xc000030550 pc=0x403ea4
github.com/notti/nocgo.callWrapper(0xc000016090, 0x10, 0x10, 0xc000000002, 0xf, 0x10, 0x7f7f6ef90008, 0x0, 0x30, 0xc000030738, ...)
~/go-projects/src/github.com/notti/nocgo/call_amd64.s:69 +0x49 fp=0xc0000305b8 sp=0xc000030588 pc=0x4a97f9
github.com/notti/nocgo.Open(0x4f4a83, 0xf, 0x40b8d8, 0x70, 0x4e71e0)
~/go-projects/src/github.com/notti/nocgo/dlopen.go:61 +0x84 fp=0xc000030610 sp=0xc0000305b8 pc=0x4a88a4
github.com/iamacarpet/go-sqlite3-dynamic.registerLibrary()
~/go-projects/src/github.com/iamacarpet/go-sqlite3-dynamic/dynamic_register_linux.go:65 +0x4b fp=0xc000030748 sp=0xc000030610 pc=0x4ace1b
github.com/iamacarpet/go-sqlite3-dynamic.init.0()
~/go-projects/src/github.com/iamacarpet/go-sqlite3-dynamic/sqlite3.go:23 +0x22 fp=0xc000030778 sp=0xc000030748 pc=0x4ae332
github.com/iamacarpet/go-sqlite3-dynamic.init()
<autogenerated>:1 +0x93 fp=0xc000030788 sp=0xc000030778 pc=0x4b4383
main.init()
<autogenerated>:1 +0x54 fp=0xc000030798 sp=0xc000030788 pc=0x4b4f84
runtime.main()
/usr/local/go/src/runtime/proc.go:188 +0x1c8 fp=0xc0000307e0 sp=0xc000030798 pc=0x42b798
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1337 +0x1 fp=0xc0000307e8 sp=0xc0000307e0 pc=0x454de1
$ ldd ./test-3
linux-vdso.so.1 (0x00007ffd0999f000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f9b75ea2000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9b75ab1000)
/lib64/ld-linux-x86-64.so.2 (0x00007f9b760a6000)
In comparison to the results on a test binary (for the ldd - I was mistaken about what I expected the results to be, but it may be relevant):
package main
import (
"fmt"
)
func main() {
fmt.Println("test")
}
And the results:
$ go build -o temp
$ ldd temp
not a dynamic executable
$ ./temp
test
Thanks for this example - with this I could reproduce it.
ldd
output is correct (with nocgo
the executable must be dynamic since libdl.so
is required).
Looks like the problem is that I need another way to detect if cgo
is available or not (github.com/notti/nocgo/fakecgo
must be imported if cgo
support is not built into the binary).
I'll try to come up with something different if possible.
@iamacarpet can you try with latest master? Should work now.