xhd2015 / xgo

All-in-one go testing library
MIT License
368 stars 19 forks source link

Bug: when running tests for pocketbase, xgo v1.0.26 fails, while xgo v1.0.25 pass #80

Open xhd2015 opened 6 months ago

xhd2015 commented 6 months ago

Fail message:

fatal error: runtime: C malloc failed
_cgo_gotypes.go:202 ...
github.com/mattn/go-sqlite3._Cfunc_CString({0xc00004f130, 0x4a})
        _cgo_gotypes.go:202 +0x1d2 fp=0xc000e663d8 sp=0xc000e662c8 pc=0x101ac2a72
github.com/mattn/go-sqlite3.(*SQLiteDriver).Open(0xc0008eaf60, {0xc00004f130, 0x4a})
        /Users/xhd2015/Projects/gopath/pkg/mod/github.com/mattn/go-sqlite3@v1.14.22/sqlite3.go:1456 +0x2a5e fp=0xc000e66920 sp=0xc000e663d8 pc=0x101af06be
database/sql.dsnConnector.Connect(...)
        /Users/xhd2015/.xgo/go-instrument/go1.21.7_Us_xh_in_go_096be049/go1.21.7/src/database/sql/sql.go:758

The line of code: name := C.CString(dsn).

After comparing xgo v1.0.26 with xgo v1.0.25, and do some bisect, found this is related to the new introduced --filepath:

fileValue := funcDecl.File   --> v1.0.26
fileValue := funcDecl.FileSyntax.Pos().RelFilename() --> v1.0.25

The actual fileValue for this package.

The stack trace shows that the file holding cgo types is _cgo_gotypes.go.

And by inserting a debug snippet into the go compiler:

relFile := f.Pos().RelFilename()
trimFile := TrimFilename(f.Pos().Base())
pkgPath := xgo_ctxt.GetPkgPath()
exec.Command("bash", "-c", fmt.Sprintf("echo 'pkg=%s relFile=%s trimFile=%s' >> /tmp/debug.log", pkgPath, relFile, trimFile)).Run()

The log shows:

pkg=github.com/mattn/go-sqlite3 relFile=/var/folders/y8/kmfy7f8s5bb5qfsp0z8h7j5m0000gq/T/go-build1192418818/b174/_cgo_gotypes.go trimFile=_cgo_gotypes.go

pkg=github.com/mattn/go-sqlite3 relFile=/var/folders/y8/kmfy7f8s5bb5qfsp0z8h7j5m0000gq/T/go-build1192418818/b174/_cgo_import.go trimFile=_cgo_import.go

Could be possibly introduced the difference between relFile and trimFile. xgo v1.0.26: use trimFile xgo v1.0.25: use relFile

xhd2015 commented 6 months ago

The cgo names:

_Cgo_ptr
_Cgo_use
_cgoCheckResult
_cgo_runtime_gobytes
_cgoCheckPointer
_cgo_runtime_gostring
_cgoexp_accf84c05a34_callbackTrampoline
_cgo_cmalloc

We should add a rule: if a function starts with _, we shoud skip it.

xhd2015 commented 6 months ago

This golang issue may be related: https://github.com/golang/go/issues/34968

Searching google: go "runtime: C malloc failed"