New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cgo: cgo treats pointer to fixed-length array as unsafe.Pointer when CC=gcc #29909

Closed
laser opened this Issue Jan 23, 2019 · 4 comments

Comments

Projects
None yet
2 participants
@laser
Copy link

laser commented Jan 23, 2019

What version of Go are you using (go version)?

go version go1.11.4 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using?

GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build457456374=/tmp/go-build -gno-record-gcc-switches"

gcc version:

gcc (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

clang version:

clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

What did you do?

First, you'll need some files:

demo.h

void wombat(const char (*xs)[31]);

demo.c

#include "demo.h"

void wombat(const char (*xs)[31]) {
  return;
}

main.go

package main

import "fmt"

// #cgo LDFLAGS: -L${SRCDIR} -ldemo
// #include <stdlib.h>
// #include "./demo.h"
import "C"

func main() {
	someGoBytes := make([]byte, 31)
	someCBytes := C.CBytes(someGoBytes[:])
	defer C.free(someCBytes)

	C.wombat((*[31]C.char)(someCBytes))

	fmt.Println("test complete")
}

What did you expect to see?

When running:

gcc -g -c demo.c -o demo.o \
  && gcc -g -shared -o libdemo.so demo.o \
  && CC=gcc go run main.go

I expected to see:

test complete

What did you see instead?

When running:

gcc -g -c demo.c -o demo.o \
  && gcc -g -shared -o libdemo.so demo.o \
  && CC=gcc go run main.go

I saw:

# command-line-arguments
./main.go:15:105: cannot use (*[31]_Ctype_char)(someCBytes) (type *[31]_Ctype_char) as type unsafe.Pointer in argument to func literal

Additional Notes

If I use clang instead of gcc, the program compiles and runs as expected.

@ianlancetaylor ianlancetaylor changed the title build error: CGO treats pointer to fixed-length array as unsafe.Pointer when CC=gcc cmd/cgo: cgo treats pointer to fixed-length array as unsafe.Pointer when CC=gcc Jan 24, 2019

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

ianlancetaylor commented Jan 24, 2019

It's unfortunate that the C compilers differ but there may not be anything we can do about this. We rely on the C compilers report of the parameter type. Since in C the type char (*)[31] decays to char ** when used as a function parameter, GCC may not preserve the information we need.

@ianlancetaylor ianlancetaylor added this to the Go1.13 milestone Jan 24, 2019

@laser

This comment has been minimized.

Copy link
Author

laser commented Jan 24, 2019

Interesting. Thanks for the quick follow-up, @ianlancetaylor .

@laser

This comment has been minimized.

Copy link
Author

laser commented Jan 24, 2019

For anyone stumbling on to this issue: The build failure does not reproduce after upgrading gcc to version 8.1.0.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

ianlancetaylor commented Jan 25, 2019

Thanks, if it's fixed with recent GCC I'm going to close this Go issue.

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