notti / nocgo Goto Github PK
View Code? Open in Web Editor NEWdlopen in go without cgo
dlopen in go without cgo
Hello @notti ,
I'm trying to test this against my SQLite3 bindings, which I've modified to add Linux support with this library and pushed to a temporary repository: https://github.com/iamacarpet/go-sqlite3-dynamic
Trying to run it, I created a new folder with main_test.go
, running with CGO_ENABLED=0 go test
package sqlite3
import (
"os"
"testing"
"database/sql"
_ "github.com/iamacarpet/go-sqlite3-dynamic"
"github.com/stretchr/testify/assert"
)
func TestXxx(t *testing.T) {
path := "test.db"
f, err := os.Create(path)
if err != nil {
t.Fatal(err)
}
f.Close()
db, err := sql.Open(`sqlite3`, path)
if err != nil {
t.Fatal(err)
}
r, err := db.Exec(`CREATE TABLE test (
id integer PRIMARY KEY NOT NULL,
name varchar(30)
)`)
if err != nil {
t.Fatal(err)
}
_ = r
r, err = db.Exec(`INSERT INTO test(name) VALUES ('first') `)
if err != nil {
t.Fatal(err)
}
rowid, err := r.LastInsertId()
if err != nil {
t.Fatal(err)
}
affected, err := r.RowsAffected()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, int64(1), rowid)
assert.Equal(t, int64(1), affected)
r, err = db.Exec(`INSERT INTO test(name) VALUES ('second') `)
if err != nil {
t.Fatal(err)
}
rowid, err = r.LastInsertId()
if err != nil {
t.Fatal(err)
}
affected, err = r.RowsAffected()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, int64(2), rowid)
assert.Equal(t, int64(1), affected)
db.Close()
os.Remove(`./test.db`)
}
It's throwing the error:
/tmp/go-build846919307/b001/sqlite3-test.test: error while loading shared libraries: /usr/lib/x86_64-linux-gnu/libpthread.so: invalid ELF header
exit status 127
FAIL sqlite3-test 0.001s
On Ubuntu 18.04, /usr/lib/x86_64-linux-gnu/libpthread.so
is an text file:
$ file /usr/lib/x86_64-linux-gnu/libpthread.so
/usr/lib/x86_64-linux-gnu/libpthread.so: ASCII text
$ cat /usr/lib/x86_64-linux-gnu/libpthread.so
/* GNU ld script
Use the shared library, but some functions are only in
the static library, so try that secondarily. */
OUTPUT_FORMAT(elf64-x86-64)
GROUP ( /lib/x86_64-linux-gnu/libpthread.so.0 /usr/lib/x86_64-linux-gnu/libpthread_nonshared.a )
I checked for references to libpthread.so
in your code and found a few.
Looking here , changing libpthread.so
to libpthread.so.0
did nothing.
When I changed all occurrences in this file, the error changes to a segfault.
$ CGO_ENABLED=0 go test
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x2 addr=0x7efe3f722fa8 pc=0x7efe3f50be19]
runtime stack:
runtime.throw(0x746fc8, 0x2a)
/usr/local/go/src/runtime/panic.go:617 +0x72
runtime.sigpanic()
/usr/local/go/src/runtime/signal_unix.go:374 +0x4a9
goroutine 1 [syscall, locked to thread]:
runtime.cgocall(0x522090, 0xc0000a7d88, 0xc0000a7da8)
/usr/local/go/src/runtime/cgocall.go:128 +0x5b fp=0xc0000a7d78 sp=0xc0000a7d40 pc=0x40418b
github.com/notti/nocgo.callWrapper(0xc000084060, 0x2a, 0x30, 0xc000000002, 0x29, 0x30, 0x7efe3f6d46d0, 0x0, 0x40b4ed, 0xc0000a7f28, ...)
~/go-projects/src/github.com/notti/nocgo/call_amd64.s:69 +0x49 fp=0xc0000a7da8 sp=0xc0000a7d78 pc=0x522069
github.com/notti/nocgo.Open(0x746546, 0x29, 0x40c248, 0x70, 0x71bdc0)
~/go-projects/src/github.com/notti/nocgo/dlopen.go:61 +0x84 fp=0xc0000a7e00 sp=0xc0000a7da8 pc=0x521114
github.com/iamacarpet/go-sqlite3-dynamic.registerLibrary()
~/go-projects/src/github.com/iamacarpet/go-sqlite3-dynamic/dynamic_register_linux.go:65 +0x4b fp=0xc0000a7f38 sp=0xc0000a7e00 pc=0x527bcb
github.com/iamacarpet/go-sqlite3-dynamic.init.0()
~/go-projects/src/github.com/iamacarpet/go-sqlite3-dynamic/sqlite3.go:23 +0x22 fp=0xc0000a7f68 sp=0xc0000a7f38 pc=0x5290e2
github.com/iamacarpet/go-sqlite3-dynamic.init()
<autogenerated>:1 +0x93 fp=0xc0000a7f78 sp=0xc0000a7f68 pc=0x52e863
sqlite3-test.init()
<autogenerated>:1 +0x54 fp=0xc0000a7f88 sp=0xc0000a7f78 pc=0x695954
main.init()
<autogenerated>:1 +0x54 fp=0xc0000a7f98 sp=0xc0000a7f88 pc=0x695bf4
runtime.main()
/usr/local/go/src/runtime/proc.go:188 +0x1c8 fp=0xc0000a7fe0 sp=0xc0000a7f98 pc=0x42ea08
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1337 +0x1 fp=0xc0000a7fe8 sp=0xc0000a7fe0 pc=0x45b481
exit status 2
FAIL sqlite3-test 0.009s
My Go version:
$ go version
go version go1.12.4 linux/amd64
Am I doing something stupid?
Regards,
iamacarpet
I see that in the README it lists Go's byte and int8 as a C char. However, the Go spec says,
byte alias for uint8
Since uint8 is from [0,255] as is an unsigned char in C I believe that Go's byte should be a C unsigned char.
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
Hey, this is an incredible piece of work, but I would feel much better about using it with a license file.
Any chance of adding a license?
If you are looking for suggestions, I would say consider MIT License.
Thanks for this great way of calling .so files without cgo.
Cameron
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.