How to cast []byte to const char buf[][8]?

Hi,

I write the following code to call C function void blsFunc(const char buf[][8]) from cgo.

/*
#cgo LDFLAGS:-lbls
#cgo linux,amd64 LDFLAGS:-L${SRCDIR}/lib/linux/amd64
#cgo darwin,amd64 LDFLAGS:-L${SRCDIR}/lib/darwin/amd64
void blsFunc(const char buf[][8]);
*/
import "C"
import (
	"unsafe"
)

func BlsFunc(buf []byte) {
	C.blsFunc((*[8]C.char)(unsafe.Pointer(&buf[0])))
}

The minimized code is https://github.com/herumi/test-travis-release .
go test -v ./bls runs well on my environments such as Ubuntu 18.04.4 + Go1.13.4 (and 1.14.1), macOS 10.15.3 + Go1.13.4, etc.
It also runs well on Travis-CI + macOS + Go1.13.4.

But it fails on Travis-CI + Linux + Go1.13.4.
https://travis-ci.org/github/herumi/test-travis-release/builds/667846719

bls/bls.go:15:118: cannot use _cgo0 (type *[8]_Ctype_char) as type unsafe.Pointer in argument to _Cfunc_blsFunc

It looks a syntax error, and I don’t know why there is a difference between my local environments and Travis despite the same version of Go.

I want to isolate which problem is my code or Travis-CI.
Could you give me some advice?

Hi, @herumi, I also don’t see the error from this. I recommend checking with the Go and/or Travis-CI team(s).

Thank you for your comment. I’ll ask Travis-CI team.

By the way, By checking the output of go tool cgo bls.go,
I found the difference between my local go and Travis-go at _obj/_cgo_gotypes.go.

// my local env and Travis-mac go

//go:cgo_unsafe_args
func _Cfunc_blsFunc(p0 *[8]_Ctype_char) (r1 _Ctype_void) {
    _cgo_runtime_cgocall(_cgo_4e5b00b212d7_Cfunc_blsFunc, uintptr(unsafe.Pointer(&p0)))
    if _Cgo_always_false {
        _Cgo_use(p0)
    }
    return
}

// Travis-linux go

//go:cgo_unsafe_args
func _Cfunc_blsFunc(p0 unsafe.Pointer) (r1 _Ctype_void) {
    _cgo_runtime_cgocall(_cgo_4e5b00b212d7_Cfunc_blsFunc, uintptr(unsafe.Pointer(&p0)))
    if _Cgo_always_false {
        _Cgo_use(p0)
    }
    return
}

The argument of _Cfunc_blsFunc of Travis-linux go is not p0 *[8]_Ctype_char but p0 unsafe.Pointer.
It may be a reason of the error.
Could you think of anything that might have caused the difference?

@herumi did you ever discover the source of this problem or find a workaround? I am getting the same build error using the xgo cross compiler for bls-eth-go-binary. Xgo container’s go is at 1.13.4: https://github.com/karalabe/xgo

I’ve not solved the problem yet.
Does the minimum sample https://github.com/herumi/test-travis-release/ run on Xgo? If not so, then I’ll check it.

Indeed, the test sample compiles with xgo for darwin, windows and linux targets and produces working binaries.

I am getting the same build error using the xgo cross compiler for bls-eth-go-binary .

I added a wrapper function to avoid this error of Travi-ci with Linux.
Could you try the latest version of GitHub - herumi/bls-eth-go-binary ?

The wrapper version builds cleanly! Thanks a bunch

:beers: Cheers!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.