-
Notifications
You must be signed in to change notification settings - Fork 18k
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: way to check if pointer satisfies (*Pinner).Pin #62356
Comments
A "Go pointer" is a pointer to memory that was allocated by Go code in the ordinary way, rather than by using something like In general it should be possible to keep track of where you allocate your pointers. Passing a C pointer (a pointer to memory allocated by C code) to |
Sure, but it is quite difficult to determine if specific |
It does seem like there is a problem here. If you are writing a general-purpose library that wraps C code, then you may want to pin the pointers being passed into your library before passing them onward to C. Pure Go general-purpose libraries can be invoked with memory that was allocated by mmap or C (provided they don't retain those pointers after returning), but this kind of C wrapper using pinner cannot. Perhaps Pin should silently allow pinning of non-Go pointers (there is nothing for it to do) instead of panicking. |
It'd fix my issue perfectly. However I'm wondering if it doesn't violate compatibility 😕 e.g. code like thisptr := C.get_my_ptr()
p := &runtime.Pinner{}
defer func() {
if r := recover() {
fmt.Println("Pointer is not go pointer")
}
}()
p.Pin(ptr)
//... |
I, at least, am not worried about that level of compatibility. |
People may don't know the detail of a pointer, this make the runtime.Pinner.Pin API easier to use. Fixes: golang#62356
People may don't know the detail of a pointer, this make the runtime.Pinner.Pin API easier to use. Fixes golang#62356
Change https://go.dev/cl/527156 mentions this issue: |
cc @golang/runtime |
I agree it's super annoying that Pin panics. The original intent was to mirror |
Looking back at the reviews for the original Pinner implementation, it looks like the possibility came up but we preferred to stick close to One thing I'm slightly concerned about is that there's no explicit escape of pointers passed to |
Just kidding. I forgot that we put the pointer into |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I'm writing a C wrapper library. I'm trying to handle
void*
pointer. I'm experiencing crashes aboutcgo pointer has Go pointer to Go pointer
, so I've updated to the latest go release (1.21) and now panic adives to usePinner
.When I use
Pinner
it works sometimes, but sometimes it crashes because ofpanic: runtime error: runtime.Pinner.Pin: argument is not a Go pointer
.I've used dlv to chack this pointer and it seems to be unsafe.Pointer as usual, so I have no idea why it panics this time.
Sadly I have no reproducable code snippet. The only thing I can post is link to my dev branch in project when this happens: https://github.com/gucio321/cimgui-go/tree/input-text-with-hint
What did you expect to see?
I think that even if this isn't a bug in Pinner, there should be some way to check if a pointer (or
unsafe.Pointer
) is a "Go Pointer"What did you see instead?
I see no way to fix my bug
I'm sory if I've used a wrong template
The text was updated successfully, but these errors were encountered: