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

cmd/cgo: function pointers are assignable #7757

Closed
DanielMorsing opened this issue Apr 10, 2014 · 9 comments
Closed

cmd/cgo: function pointers are assignable #7757

DanielMorsing opened this issue Apr 10, 2014 · 9 comments

Comments

@DanielMorsing
Copy link
Contributor

Function pointers from the cgo package are assignable. I'd expect them to be constants.

example:

package main

/*
#include <stdlib.h>
char *foo() { return NULL; }
*/
import "C"

import "fmt"
import "unsafe"

func main() {
    C.foo = unsafe.Pointer(uintptr(2))
    fmt.Printf("%v\n", C.foo)
}

output:
0x2
@ianlancetaylor
Copy link
Contributor

Comment 1:

I suppose we could detect that case in rewriteRef and have cgo give an error.

Labels changed: added repo-main, release-none.

@DanielMorsing
Copy link
Contributor Author

Comment 2:

Would it be possible to emit a constant instead?

@ianlancetaylor
Copy link
Contributor

Comment 3:

How could we emit a constant?  cgo is generating Go code, and there is no Go constant
that can represent a C function pointer.

@DanielMorsing
Copy link
Contributor Author

Comment 4:

cgo uses unsafe.Pointer for function pointers. You can make a constant one of those.
http://play.golang.org/p/ked5QOYfx8

@DanielMorsing
Copy link
Contributor Author

Comment 5:

I doubt that the code is there for figuring out the eventual address of the function
entry though. rewriteRef route is probably easier to implement.

@ianlancetaylor
Copy link
Contributor

Comment 6:

Interesting example in comment #4.  Filed issue #7760.

@mdempsky
Copy link
Member

mdempsky commented Aug 6, 2014

Comment 7:

I don't think we can use Go consts here, because they need to be known at compile-time,
whereas a C function's address won't be known at least until link-time (and possibly not
until run-time).
However, two possible ways to address this problem (both of which ultimately defer to
the Go compiler to reject assignments to C.foo):
1. Instead of emitting C.foo as "_Cfpvar_fp_foo", emit it as
"unsafe.Pointer(_Cfpvar_fp_foo)".  The explicit conversion would make it
non-addressable.  (I'd probably actually emit as "_Cgo_pointer(_Cfpvar_fp_foo)" and
change the "type _ unsafe.Pointer" in _cgo_gotypes.go to "type _Cgo_pointer
unsafe.Pointer" to avoid worrying about whether package "import" is available in the
user code.)
2. Emit it as a function call like "_Cfpvar_fp_foo()" and change _Cfpvar_fp_foo into a C
function that returns the address of 'foo'.

@gopherbot
Copy link

Comment 8:

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

@ianlancetaylor
Copy link
Contributor

Comment 9:

This issue was closed by revision 11016f6.

Status changed to Fixed.

@golang golang locked and limited conversation to collaborators Jun 25, 2016
wheatman pushed a commit to wheatman/go-akaros that referenced this issue Jun 25, 2018
wheatman pushed a commit to wheatman/go-akaros that referenced this issue Jul 9, 2018
This issue was closed.
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

4 participants