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: found bad pointer in Go heap (incorrect use of unsafe or cgo?) #19135
Comments
Are you using Go 1.7 or Go 1.8? Your bug report is unclear. /cc @ianlancetaylor |
1.8, sorry. I'll edit the description. I've also tested against tip and it shows the same issue. It worked fine with 1.7.5. |
The error mentions cgo because that is a common cause of this kind of problem, but this is not a cgo check as such. This is an error from the garbage collector, telling you that it found a dangling pointer. Specifically the garbage collector came across a pointer with the value I will look more later, have to go now. |
I just don't understand how it's possible that |
I think the problem is that your As far as I can tell your code is actually technically invalid according to the cgo rules, because you are storing a Go pointer in C memory. That is not permitted. It's not exactly what is causing this problem, though. The best fix for this problem is probably going to be for you to write a little wrapper that receives a Go pointer, stores it in the I'm going to close this because I don't think there is anything we can do on the Go side to help. Please comment if you disagree. |
That's exactly what this function is doing. It's passing a Go pointer to the |
And yes, |
I'm sorry, I misunderstood. You want me to make the wrapper in C, not Go, so that there's no possibility of C holding on to the Go pointer after the function call to the wrapper. I've just given that a shot and it seems to work. Still no idea why this is only triggered if I nulled out that pointer, though. |
It's triggered because when you write |
Very interesting read. Why does Go need to worry about GC at all for memory that's not allocated by Go? In my example the memory in question exists on the C heap due to the allocation from |
Yes, the memory being changed is in the C heap. But the value being changed, the value stored in that memory, is a Go pointer. On line 98 you stored a Go pointer--a pointer to memory allocated by the Go memory allocator--into memory allocated by the C allocator. As I said earlier, that in itself is invalid: Go code is not permitted to store a Go pointer in C memory, even temporarily. But nothing catches that (I think it would be caught if you set Then later, while in this invalid state, you store |
This has been very enlightening. Thanks for your time! |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?go version go1.8 darwin/386
What operating system and processor architecture are you using (
go env
)?OS X 10.11.6, and compiling my code as darwin/386 because I need a 32-bit shared library.
What did you do?
I've got the following package that's a wrapper for libz, which I've been using for years without issue. Once upgrading to Go 1.8, I'm getting runtime panics, due to aggressive cgo checks. The panic happens on line 91 (marked in comments) where I try to make sure that I don't leave a Go pointer to
p
in C memory after theRead
function ends. The funny thing is, if I comment this line out I don't have any problems, even though I'm technically violating the rules.Another thing, I've noticed is that if I set the buffer length on line 98 (marked in comments) to one less than the actual size of the buffer I don't have a problem. What I suspect is happening is that zlib is incrementing
next_out
after reading. If it's read the entire buffer, that pointer is now pointing to memory right after the go slice. If I set the length of the buffer to be one byte short, then that pointer is still valid memory.Either way, we are talking about pointers on the C heap, so I don't know why I'm getting a panic when setting that pointer to NULL. Commenting out line 91 also fixes the issue.
What did you see instead?
The text was updated successfully, but these errors were encountered: