-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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/compile: fatal error: checkptr: pointer arithmetic computed bad pointer value #64467
Comments
Your code violates the 3rd rule: https://pkg.go.dev/unsafe#Pointer |
So maybe it shouldn't apply to packages that use C or something like this? |
Timed out in state WaitingForInfo. Closing. (I am just a bot, though. Please speak up if this is a mistake or you have the requested information.) |
@gopherbot it shouldn't be closed CC @dmitshur |
Hi @gucio321, can you clarify why this shouldn’t be closed? Can you also comment more directly on this:
Do you agree with that assessment? Also, note that the Go project does not use the issue tracker for answering questions: |
Hi, thank you for your reply! If we consider solution from #64111 the only possible (and we should because the issue was closed as completed) it seems logical to me that this issue needs separated fix and should not be closed. In my opinion rhe solution here would be either
I hope I've justifies my point of view. Lmk if something is unclear or i need to provide some more info! |
You can certainly pass it as a Similarly for the return value. Wrap (in C) the thing that returns a
What rule are you referring to here? (A reference link would be great.) |
I'm sorry - I forgot to put it there. I mean the rules mentioned in 1st comment on this thread. Ref link: https://pkg.go.dev/unsafe#Pointer
Well, so this is a workaround, however its (in my opinion) a bit stupid that user can't properly handle a normal thing (a void* C pointer) using GO stuff and they need to do the trick on C side... [digression: shouldn't I still view that this issue needs some fix in GO. |
Hi @gucio321, "stupid" might be an overly strong word to choose? FWIW, I think cgo is tackling a fairly difficult and nuanced set of problems. In #64111, you reported the error:
and you mentioned towards the end of that discussion that:
In that issue, it's not that the Lines 621 to 626 in b25f555
Given the Go runtime implements growable stacks, it has to adjust pointers in some cases, including so that pointers to stack locations are still valid after the stack has been copied/moved. While adjusting pointers, in your case it seems the runtime found what looks to be an illegal Go pointer, and it complains about that as an important diagnostic to help people avoid "Heisenbugs", intermittent crashes, or subtle data corruptions. In this issue here, it's also a deliberate diagnostic check that is complaining about the violation of the unsafe rules (in this case, additional pointer checks that are automatically enabled when running with the race detector). When these diagnostics were first introduced, the reaction was extremely positive, such as in this excellent blog entry from then:
(That write-up is worth a read if you haven't already read it. It's by Matt Layher, a heavy user of unsafe & cgo from the broader community). In any event, you don't seem to be encountering a bug in Go, but rather automatic enforcement of an important set of rules. As the cgo documentation says:
Fortunately, Go does seem to support what you want to do. As Bryan said in #64111 (comment):
You mentioned above:
If you don't want to change your actual C code, could you have some wrappers or small conversion utilities in the cgo preamble of one of your .go files, which is a nice convenience offered by cgo for such things? If the Go side never needs to interpret that value as a Go pointer, can it always be a uintptr on the Go side? |
Thank you for this detailed answer!
|
This comment was marked as outdated.
This comment was marked as outdated.
Hi @gucio321, I tried to explain that above in #64467 (comment) in the part about the runtime adjusting pointers it finds on the stack in some cases. It does not require Here's a quick example from scratch that doesn't use https://go.dev/play/p/d09eyqUlVV0 func main() {
stretchStack(16, 0) // use ~16 KiB of stack.
p := badPointer() // store unsafe.Pointer with value 0x1 on stack.
runtime.GC() // trigger stack shrinking and discovery of bad pointer on stack (usually).
foo(p)
} Running that gives:
Note that the error stack includes:
|
ok, I see thank you.
|
currently it applies to cimgui functions and typedefs. What is missing is the struct_accessor that will be reworked in the next commit. Rason for that is golang/go#64467
Go version
go version go1.21.3 linux/amd64
What operating system and processor architecture are you using (
go env
)?What did you do?
I want to cast an unintptr to unsafe.Pointer.
When running with
-race
it crashes:Here is the code
What did you expect to see?
No panic when running with -race
In reference to #64111 as a solution of that issue I was suggested to use uintptr as an alternative to unsafe.pointer.
However I need to cast it (for a while) to unsafe.pointer to be able to pass it to C function taking
void*
What did you see instead?
The text was updated successfully, but these errors were encountered: