@@ -238,7 +238,7 @@ the type of the pointer.
238
238
239
239
Go code may pass a Go pointer to C provided the Go memory to which it
240
240
points does not contain any Go pointers. The C code must preserve
241
- this property: it must not store any Go pointers into Go memory, even
241
+ this property: it must not store any Go pointers in Go memory, even
242
242
temporarily. When passing a pointer to a field in a struct, the Go
243
243
memory in question is the memory occupied by the field, not the entire
244
244
struct. When passing a pointer to an element in an array or slice,
@@ -247,25 +247,29 @@ array of the slice.
247
247
248
248
C code may not keep a copy of a Go pointer after the call returns.
249
249
250
- If Go code passes a Go pointer to a C function, the C function must
251
- return. There is no specific time limit, but a C function that simply
252
- blocks holding a Go pointer while other goroutines are running may
253
- eventually cause the program to run out of memory and fail (because
254
- the garbage collector may not be able to make progress).
255
-
256
250
A Go function called by C code may not return a Go pointer. A Go
257
251
function called by C code may take C pointers as arguments, and it may
258
252
store non-pointer or C pointer data through those pointers, but it may
259
- not store a Go pointer into memory pointed to by a C pointer. A Go
253
+ not store a Go pointer in memory pointed to by a C pointer. A Go
260
254
function called by C code may take a Go pointer as an argument, but it
261
255
must preserve the property that the Go memory to which it points does
262
256
not contain any Go pointers.
263
257
264
- These rules are partially enforced by cgo by default. It is possible
265
- to defeat this enforcement by using the unsafe package, and of course
266
- there is nothing stopping the C code from doing anything it likes.
267
- However, programs that break these rules are likely to fail in
268
- unexpected and unpredictable ways.
258
+ Go code may not store a Go pointer in C memory. C code may store Go
259
+ pointers in C memory, subject to the rule above: it must stop storing
260
+ the Go pointer when the C function returns.
261
+
262
+ These rules are checked dynamically at runtime. The checking is
263
+ controlled by the cgocheck setting of the GODEBUG environment
264
+ variable. The default setting is GODEBUG=cgocheck=1, which implements
265
+ reasonably cheap dynamic checks. These checks may be disabled
266
+ entirely using GODEBUG=cgocheck=0. Complete checking of pointer
267
+ handling, at some cost in run time, is available via GODEBUG=cgocheck=2.
268
+
269
+ It is possible to defeat this enforcement by using the unsafe package,
270
+ and of course there is nothing stopping the C code from doing anything
271
+ it likes. However, programs that break these rules are likely to fail
272
+ in unexpected and unpredictable ways.
269
273
270
274
Using cgo directly
271
275
0 commit comments