Skip to content
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

runtime: runtime error: cgo argument has Go pointer to Go pointer #14483

Closed
alexbrainman opened this issue Feb 23, 2016 · 6 comments
Closed

runtime: runtime error: cgo argument has Go pointer to Go pointer #14483

alexbrainman opened this issue Feb 23, 2016 · 6 comments

Comments

@alexbrainman
Copy link
Member

I had a crash alexbrainman/odbc#65 after compiling my program with latest version of Go.

I simplified my crash down to this:

# go version
go version devel +e7524d5 Tue Feb 23 03:42:38 2016 +0000 linux/386
# cat -n main.go
     1  package main
     2
     3  import (
     4          "unsafe"
     5  )
     6
     7  /*
     8  typedef void* VOIDPTR;
     9
    10  void cfunc(VOIDPTR p) {
    11  }
    12  */
    13  import "C"
    14
    15  type stru struct {
    16          a   []byte
    17          buf [8]byte
    18  }
    19
    20  func main() {
    21          var s stru
    22          s.a = s.buf[:]
    23          C.cfunc(C.VOIDPTR(unsafe.Pointer(&s.buf[0])))
    24  }
# go run main.go
panic: runtime error: cgo argument has Go pointer to Go pointer

goroutine 1 [running]:
panic(0x809e300, 0x1830a040)
        /root/go/src/runtime/panic.go:483 +0x331
main.main()
        /root/src/t/main.go:23 +0x8c
exit status 2
#

I don't undestand what this program doing wrong that CGO function parmeter checker crashed it. Can someone explain it to me, please? Also, if I remove line 22, the program runs with no problem. Why?

Thank you.

I suspect @ianlancetaylor will be interested.

Alex

@bradfitz
Copy link
Contributor

I believe this is because the 8 bytes are in the same memory allocation, contiguous with the pointer in the slice of the a field. The cgo checker doesn't know you're only sharing memory to the *byte, as it only sees the address and the address is within a range which contains Go pointers.

@ianlancetaylor
Copy link
Contributor

Hmmm, it works fine if you remove the C.VOIDPTR type conversion. I'll look into this.

@ianlancetaylor ianlancetaylor added this to the Go1.6.1 milestone Feb 23, 2016
@ianlancetaylor ianlancetaylor self-assigned this Feb 23, 2016
@alexbrainman
Copy link
Member Author

it works fine if you remove the C.VOIDPTR type conversion

It does indeed.

Alex

@ianlancetaylor
Copy link
Contributor

@bradfitz By the way, this kind of thing is supposed to work if the address operation is one of the direct arguments to the C call. In that specific case, cgo is supposed to know that only that particular field needs to be checked for pointers, not the entire object to which the pointer points.

@gopherbot
Copy link

CL https://golang.org/cl/19860 mentions this issue.

@gopherbot
Copy link

CL https://golang.org/cl/22041 mentions this issue.

gopherbot pushed a commit that referenced this issue Apr 14, 2016
Fixes #14483.

Change-Id: I0cddfe27fd8d00ba85659d0b618410e39ebf45cb
Reviewed-on: https://go-review.googlesource.com/19860
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-on: https://go-review.googlesource.com/22041
Reviewed-by: Ian Lance Taylor <iant@golang.org>
@golang golang locked and limited conversation to collaborators Apr 19, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants