{"action":"created","issue":{"url":"https://api.github.com/repos/golang/go/issues/12416","labels_url":"https://api.github.com/repos/golang/go/issues/12416/labels{/name}","comments_url":"https://api.github.com/repos/golang/go/issues/12416/comments","events_url":"https://api.github.com/repos/golang/go/issues/12416/events","html_url":"https://github.com/golang/go/issues/12416","id":104096275,"number":12416,"title":"proposal: specific rules for passing pointers between Go and C","user":{"login":"ianlancetaylor","id":3194333,"avatar_url":"https://avatars.githubusercontent.com/u/3194333?v=3","gravatar_id":"","url":"https://api.github.com/users/ianlancetaylor","html_url":"https://github.com/ianlancetaylor","followers_url":"https://api.github.com/users/ianlancetaylor/followers","following_url":"https://api.github.com/users/ianlancetaylor/following{/other_user}","gists_url":"https://api.github.com/users/ianlancetaylor/gists{/gist_id}","starred_url":"https://api.github.com/users/ianlancetaylor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ianlancetaylor/subscriptions","organizations_url":"https://api.github.com/users/ianlancetaylor/orgs","repos_url":"https://api.github.com/users/ianlancetaylor/repos","events_url":"https://api.github.com/users/ianlancetaylor/events{/privacy}","received_events_url":"https://api.github.com/users/ianlancetaylor/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/golang/go/labels/Proposal","name":"Proposal","color":"ededed"}],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/golang/go/milestones/24","html_url":"https://github.com/golang/go/milestones/Go1.6","labels_url":"https://api.github.com/repos/golang/go/milestones/24/labels","id":1096159,"number":24,"title":"Go1.6","description":"","creator":{"login":"josharian","id":67496,"avatar_url":"https://avatars.githubusercontent.com/u/67496?v=3","gravatar_id":"","url":"https://api.github.com/users/josharian","html_url":"https://github.com/josharian","followers_url":"https://api.github.com/users/josharian/followers","following_url":"https://api.github.com/users/josharian/following{/other_user}","gists_url":"https://api.github.com/users/josharian/gists{/gist_id}","starred_url":"https://api.github.com/users/josharian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/josharian/subscriptions","organizations_url":"https://api.github.com/users/josharian/orgs","repos_url":"https://api.github.com/users/josharian/repos","events_url":"https://api.github.com/users/josharian/events{/privacy}","received_events_url":"https://api.github.com/users/josharian/received_events","type":"User","site_admin":false},"open_issues":302,"closed_issues":86,"state":"open","created_at":"2015-05-05T02:39:06Z","updated_at":"2015-08-31T17:30:44Z","due_on":"2016-01-31T08:00:00Z","closed_at":null},"comments":2,"created_at":"2015-08-31T17:30:44Z","updated_at":"2015-08-31T17:56:33Z","closed_at":null,"body":"Issues #12116 and #8310 discuss the problem of when it is OK to pass pointers between Go and C code when using cgo.  This proposal is a specific set of rules for when it is OK, plus some modifications to cgo to partially enforce the rules.\r\n\r\nDefinitions: a Go pointer is a pointer to memory allocated by the Go runtime.  A C pointer is a pointer to memory not allocated by the Go runtime, such as by C.malloc.\r\n\r\nThe proposed rules are:\r\n\r\n* Go code may pass a Go pointer to C provided that the Go memory to which it points does not contain any Go pointers.  That rule must be preserved during C execution, in that the program must not store any Go pointers into that memory.\r\n\r\n* C code may otherwise use the Go pointer and the Go memory to which it points freely during the call.  However, C code may not keep a copy of the Go pointer after the call returns.\r\n\r\n* If Go code passes a Go pointer to a C function, the C function must return.  While there are no documented time limits, a C function that simply blocks holding a Go pointer while other goroutines are running may eventually cause the program to run out of memory and fail.\r\n\r\n* A Go function called by C code may not return a Go pointer.  A Go function called by C code may take C pointers as arguments, and it may store non-pointer or C pointer data through those pointers, but it may not store a Go pointer into memory pointed to by a C pointer.  A Go function called by C code may take a Go pointer as an argument, but it must preserve the property that the Go memory to which it points does not contain any Go pointers.\r\n\r\nThe proposal is that we declare that all Go programs that adhere to those rules are, and will remain, valid, under the Go 1 compatibility guarantee.  Go programs that do not adhere to these rules may break today or in future releases.\r\n\r\nWe further propose some modifications to cgo that will help enforce these rules.  The cgo changes are not the rules; the rules are as above.  The cgo changes are intended to make it easier to detect, at build time, programs that violate the rules.\r\n\r\n* We modify cgo to prohibit any type conversion from a Go pointer type to a C pointer type, except as a direct argument to a C function call.  Any such type conversion is an error in cgo.  Even within a direct function call, we prohibit type conversions if the Go pointer type points to a type that itself contains pointers.\r\n\r\n* A function labelled //export, which may be called from C code, must not return any Go pointer type and must not have any parameters that are Go pointer types.\r\n\r\nThese cgo rules are more strict than the rules about passing pointers.  They will prohibit cases that are permitted.  It is also possible to work around them, even without using unsafe, and write code that is not blocked by cgo but that violates the above rules.  I will provide examples in a separate design doc."},"comment":{"url":"https://api.github.com/repos/golang/go/issues/comments/136444656","html_url":"https://github.com/golang/go/issues/12416#issuecomment-136444656","issue_url":"https://api.github.com/repos/golang/go/issues/12416","id":136444656,"user":{"login":"griesemer","id":8528975,"avatar_url":"https://avatars.githubusercontent.com/u/8528975?v=3","gravatar_id":"","url":"https://api.github.com/users/griesemer","html_url":"https://github.com/griesemer","followers_url":"https://api.github.com/users/griesemer/followers","following_url":"https://api.github.com/users/griesemer/following{/other_user}","gists_url":"https://api.github.com/users/griesemer/gists{/gist_id}","starred_url":"https://api.github.com/users/griesemer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/griesemer/subscriptions","organizations_url":"https://api.github.com/users/griesemer/orgs","repos_url":"https://api.github.com/users/griesemer/repos","events_url":"https://api.github.com/users/griesemer/events{/privacy}","received_events_url":"https://api.github.com/users/griesemer/received_events","type":"User","site_admin":false},"created_at":"2015-08-31T17:56:33Z","updated_at":"2015-08-31T17:56:33Z","body":"Presumably this requires either a) a GC that doesn't move objects; or b) a form or (automatic, via CGO) pinning of Go pointers passed to C."}}