-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: Writing to C pointer causes Go heap corruption #26288
Comments
"Heap corruption" isn't really the right term here, or at least that's not a conclusion I can draw yet. Go is finding a pointer in its heap that is bad somehow (not a valid Go or C pointer). Presumably it's the result of So where does the result of The reason I ask is that the bad pointer is to the very start of an object span. That makes me think the pointer points just past the end of the last object in the previous span. That can happen if you do pointer arithmetic with |
Go code is never permitted to store a Go pointer into memory allocated by C. See https://golang.org/cmd/cgo/#hdr-Passing_pointers . Try running your program with the environment variable |
Sorry for the distraction with
I know that and really try not to. I even started copying byte arrays before passing to C. The I discovered one thing. If I modify the function by adding
I sometimes get different panic error: runtime: nelems=128 nalloc=107 previous allocCount=106 nfreed=65535
fatal error: sweep increased allocation count
runtime stack:
runtime.throw(0x8953ab, 0x20)
/usr/lib/go-1.10/src/runtime/panic.go:616 +0x81
runtime.(*mspan).sweep(0x7f2b5c33a708, 0x7f2b5c33a701, 0x7ffe3ae0b600)
/usr/lib/go-1.10/src/runtime/mgcsweep.go:298 +0x89c
runtime.(*mcentral).cacheSpan(0xd2c870, 0x7f2b4274cf68)
/usr/lib/go-1.10/src/runtime/mcentral.go:58 +0x277
runtime.(*mcache).refill(0x7f2b5c376000, 0xd1b80a)
/usr/lib/go-1.10/src/runtime/mcache.go:123 +0x9c
runtime.(*mcache).nextFree.func1()
/usr/lib/go-1.10/src/runtime/malloc.go:556 +0x32
runtime.systemstack(0x0)
/usr/lib/go-1.10/src/runtime/asm_amd64.s:409 +0x79
runtime.mstart()
/usr/lib/go-1.10/src/runtime/proc.go:1175 |
Can you show us a complete program that we can use to recreate the problem ourselves? |
The program is too big probably for that, but the commit that fixed the issue is here: ethereum/go-ethereum@8a99766. I can provide more detailed instructions how to run it if you want to. |
Thanks. Is it possible that the C memory passed to the exported |
Yes, it's uninitiated struct allocated on C stack. |
Duplicate of #19928. |
What version of Go are you using (
go version
)?go version go1.10.1 linux/amd64
Does this issue reproduce with the latest release?
I have not checked 1.10.3 nor 1.11 beta.
What operating system and processor architecture are you using (
go env
)?What did you do?
I have an Go exported function that takes a pointer to a C struct as an argument. It is so-called output
argument, so the Go function is writing data to this C struct via the provided pointer.
Depending how exactly this is done the Go heap might get corrupted.
I have not minimized the example fully.
The C struct is:
The variant of the exported function working correctly:
Go assembly:
The variant of the exported function causing the following Go heap corruption:
Go assembly:
The text was updated successfully, but these errors were encountered: