| 1 |
// Copyright 2009 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// Cgo call and callback support. |
| 6 |
|
| 7 |
package runtime |
| 8 |
|
| 9 |
import ( |
| 10 |
"internal/goarch" |
| 11 |
"unsafe" |
| 12 |
) |
| 13 |
|
| 14 |
// Functions called by cgo-generated code. |
| 15 |
//go:linkname cgoCheckPointer |
| 16 |
//go:linkname cgoCheckResult |
| 17 |
|
| 18 |
var ncgocall uint64 // number of cgo calls in total for dead m |
| 19 |
|
| 20 |
// Pointer checking for cgo code. |
| 21 |
|
| 22 |
// We want to detect all cases where a program that does not use |
| 23 |
// unsafe makes a cgo call passing a Go pointer to memory that |
| 24 |
// contains a Go pointer. Here a Go pointer is defined as a pointer |
| 25 |
// to memory allocated by the Go runtime. Programs that use unsafe |
| 26 |
// can evade this restriction easily, so we don't try to catch them. |
| 27 |
// The cgo program will rewrite all possibly bad pointer arguments to |
| 28 |
// call cgoCheckPointer, where we can catch cases of a Go pointer |
| 29 |
// pointing to a Go pointer. |
| 30 |
|
| 31 |
// Complicating matters, taking the address of a slice or array |
| 32 |
// element permits the C program to access all elements of the slice |
| 33 |
// or array. In that case we will see a pointer to a single element, |
| 34 |
// but we need to check the entire data structure. |
| 35 |
|
| 36 |
// The cgoCheckPointer call takes additional arguments indicating that |
| 37 |
// it was called on an address expression. An additional argument of |
| 38 |
// true means that it only needs to check a single element. An |
| 39 |
// additional argument of a slice or array means that it needs to |
| 40 |
// check the entire slice/array, but nothing else. Otherwise, the |
| 41 |
// pointer could be anything, and we check the entire heap object, |
| 42 |
// which is conservative but safe. |
| 43 |
|
| 44 |
// When and if we implement a moving garbage collector, |
| 45 |
// cgoCheckPointer will pin the pointer for the duration of the cgo |
| 46 |
// call. (This is necessary but not sufficient; the cgo program will |
| 47 |
// also have to change to pin Go pointers that cannot point to Go |
| 48 |
// pointers.) |
| 49 |
|
| 50 |
// cgoCheckPointer checks if the argument contains a Go pointer that |
| 51 |
// points to a Go pointer, and panics if it does. |
| 52 |
func cgoCheckPointer(ptr any, arg any) { |
| 53 |
if debug.cgocheck == 0 { |
| 54 |
return |
| 55 |
} |
| 56 |
|
| 57 |
ep := efaceOf(&ptr) |
| 58 |
t := ep._type |
| 59 |
|
| 60 |
top := true |
| 61 |
if arg != nil && (t.kind&kindMask == kindPtr || t.kind&kindMask == kindUnsafePointer) { |
| 62 |
p := ep.data |
| 63 |
if t.kind&kindDirectIface == 0 { |
| 64 |
p = *(*unsafe.Pointer)(p) |
| 65 |
} |
| 66 |
if p == nil || !cgoIsGoPointer(p) { |
| 67 |
return |
| 68 |
} |
| 69 |
aep := efaceOf(&arg) |
| 70 |
switch aep._type.kind & kindMask { |
| 71 |
case kindBool: |
| 72 |
if t.kind&kindMask == kindUnsafePointer { |
| 73 |
// We don't know the type of the element. |
| 74 |
break |
| 75 |
} |
| 76 |
pt := (*ptrtype)(unsafe.Pointer(t)) |
| 77 |
cgoCheckArg(pt.elem, p, true, false, cgoCheckPointerFail) |
| 78 |
return |
| 79 |
case kindSlice: |
| 80 |
// Check the slice rather than the pointer. |
| 81 |
ep = aep |
| 82 |
t = ep._type |
| 83 |
case kindArray: |
| 84 |
// Check the array rather than the pointer. |
| 85 |
// Pass top as false since we have a pointer |
| 86 |
// to the array. |
| 87 |
ep = aep |
| 88 |
t = ep._type |
| 89 |
top = false |
| 90 |
default: |
| 91 |
throw("can't happen") |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
cgoCheckArg(t, ep.data, t.kind&kindDirectIface == 0, top, cgoCheckPointerFail) |
| 96 |
} |
| 97 |
|
| 98 |
const cgoCheckPointerFail = "cgo argument has Go pointer to Go pointer" |
| 99 |
const cgoResultFail = "cgo result has Go pointer" |
| 100 |
|
| 101 |
// cgoCheckArg is the real work of cgoCheckPointer. The argument p |
| 102 |
// is either a pointer to the value (of type t), or the value itself, |
| 103 |
// depending on indir. The top parameter is whether we are at the top |
| 104 |
// level, where Go pointers are allowed. |
| 105 |
func cgoCheckArg(t *_type, p unsafe.Pointer, indir, top bool, msg string) { |
| 106 |
if t.ptrdata == 0 || p == nil { |
| 107 |
// If the type has no pointers there is nothing to do. |
| 108 |
return |
| 109 |
} |
| 110 |
|
| 111 |
switch t.kind & kindMask { |
| 112 |
default: |
| 113 |
throw("can't happen") |
| 114 |
case kindArray: |
| 115 |
at := (*arraytype)(unsafe.Pointer(t)) |
| 116 |
if !indir { |
| 117 |
if at.len != 1 { |
| 118 |
throw("can't happen") |
| 119 |
} |
| 120 |
cgoCheckArg(at.elem, p, at.elem.kind&kindDirectIface == 0, top, msg) |
| 121 |
return |
| 122 |
} |
| 123 |
for i := uintptr(0); i < at.len; i++ { |
| 124 |
cgoCheckArg(at.elem, p, true, top, msg) |
| 125 |
p = add(p, at.elem.size) |
| 126 |
} |
| 127 |
case kindChan, kindMap: |
| 128 |
// These types contain internal pointers that will |
| 129 |
// always be allocated in the Go heap. It's never OK |
| 130 |
// to pass them to C. |
| 131 |
panic(errorString(msg)) |
| 132 |
case kindFunc: |
| 133 |
if indir { |
| 134 |
p = *(*unsafe.Pointer)(p) |
| 135 |
} |
| 136 |
if !cgoIsGoPointer(p) { |
| 137 |
return |
| 138 |
} |
| 139 |
panic(errorString(msg)) |
| 140 |
case kindInterface: |
| 141 |
it := *(**_type)(p) |
| 142 |
if it == nil { |
| 143 |
return |
| 144 |
} |
| 145 |
// A type known at compile time is OK since it's |
| 146 |
// constant. A type not known at compile time will be |
| 147 |
// in the heap and will not be OK. |
| 148 |
if inheap(uintptr(unsafe.Pointer(it))) { |
| 149 |
panic(errorString(msg)) |
| 150 |
} |
| 151 |
p = *(*unsafe.Pointer)(add(p, goarch.PtrSize)) |
| 152 |
if !cgoIsGoPointer(p) { |
| 153 |
return |
| 154 |
} |
| 155 |
if !top { |
| 156 |
panic(errorString(msg)) |
| 157 |
} |
| 158 |
cgoCheckArg(it, p, it.kind&kindDirectIface == 0, false, msg) |
| 159 |
case kindSlice: |
| 160 |
st := (*slicetype)(unsafe.Pointer(t)) |
| 161 |
s := (*slice)(p) |
| 162 |
p = s.array |
| 163 |
if p == nil || !cgoIsGoPointer(p) { |
| 164 |
return |
| 165 |
} |
| 166 |
if !top { |
| 167 |
panic(errorString(msg)) |
| 168 |
} |
| 169 |
if st.elem.ptrdata == 0 { |
| 170 |
return |
| 171 |
} |
| 172 |
for i := 0; i < s.cap; i++ { |
| 173 |
cgoCheckArg(st.elem, p, true, false, msg) |
| 174 |
p = add(p, st.elem.size) |
| 175 |
} |
| 176 |
case kindString: |
| 177 |
ss := (*stringStruct)(p) |
| 178 |
if !cgoIsGoPointer(ss.str) { |
| 179 |
return |
| 180 |
} |
| 181 |
if !top { |
| 182 |
panic(errorString(msg)) |
| 183 |
} |
| 184 |
case kindStruct: |
| 185 |
st := (*structtype)(unsafe.Pointer(t)) |
| 186 |
if !indir { |
| 187 |
if len(st.fields) != 1 { |
| 188 |
throw("can't happen") |
| 189 |
} |
| 190 |
cgoCheckArg(st.fields[0].typ, p, st.fields[0].typ.kind&kindDirectIface == 0, top, msg) |
| 191 |
return |
| 192 |
} |
| 193 |
for _, f := range st.fields { |
| 194 |
if f.typ.ptrdata == 0 { |
| 195 |
continue |
| 196 |
} |
| 197 |
cgoCheckArg(f.typ, add(p, f.offset()), true, top, msg) |
| 198 |
} |
| 199 |
case kindPtr, kindUnsafePointer: |
| 200 |
if indir { |
| 201 |
p = *(*unsafe.Pointer)(p) |
| 202 |
if p == nil { |
| 203 |
return |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
if !cgoIsGoPointer(p) { |
| 208 |
return |
| 209 |
} |
| 210 |
if !top { |
| 211 |
panic(errorString(msg)) |
| 212 |
} |
| 213 |
|
| 214 |
cgoCheckUnknownPointer(p, msg) |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
// cgoCheckUnknownPointer is called for an arbitrary pointer into Go |
| 219 |
// memory. It checks whether that Go memory contains any other |
| 220 |
// pointer into Go memory. If it does, we panic. |
| 221 |
// The return values are unused but useful to see in panic tracebacks. |
| 222 |
func cgoCheckUnknownPointer(p unsafe.Pointer, msg string) (base, i uintptr) { |
| 223 |
if inheap(uintptr(p)) { |
| 224 |
b, span, _ := findObject(uintptr(p), 0, 0, false) |
| 225 |
base = b |
| 226 |
if base == 0 { |
| 227 |
return |
| 228 |
} |
| 229 |
hbits := heapBitsForAddr(base) |
| 230 |
n := span.elemsize |
| 231 |
for i = uintptr(0); i < n; i += goarch.PtrSize { |
| 232 |
if !hbits.morePointers() { |
| 233 |
// No more possible pointers. |
| 234 |
break |
| 235 |
} |
| 236 |
if hbits.isPointer() && cgoIsGoPointer(*(*unsafe.Pointer)(unsafe.Pointer(base + i))) { |
| 237 |
panic(errorString(msg)) |
| 238 |
} |
| 239 |
hbits = hbits.next() |
| 240 |
} |
| 241 |
|
| 242 |
return |
| 243 |
} |
| 244 |
|
| 245 |
lo := 0 |
| 246 |
hi := len(gcRootsIndex) |
| 247 |
for lo < hi { |
| 248 |
m := lo + (hi-lo)/2 |
| 249 |
pr := gcRootsIndex[m] |
| 250 |
addr := uintptr(pr.decl) |
| 251 |
if cgoInRange(p, addr, addr+pr.size) { |
| 252 |
cgoCheckBits(pr.decl, pr.gcdata, 0, pr.ptrdata) |
| 253 |
return |
| 254 |
} |
| 255 |
if uintptr(p) < addr { |
| 256 |
hi = m |
| 257 |
} else { |
| 258 |
lo = m + 1 |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
return |
| 263 |
} |
| 264 |
|
| 265 |
// cgoIsGoPointer reports whether the pointer is a Go pointer--a |
| 266 |
// pointer to Go memory. We only care about Go memory that might |
| 267 |
// contain pointers. |
| 268 |
//go:nosplit |
| 269 |
//go:nowritebarrierrec |
| 270 |
func cgoIsGoPointer(p unsafe.Pointer) bool { |
| 271 |
if p == nil { |
| 272 |
return false |
| 273 |
} |
| 274 |
|
| 275 |
if inHeapOrStack(uintptr(p)) { |
| 276 |
return true |
| 277 |
} |
| 278 |
|
| 279 |
roots := gcRoots |
| 280 |
for roots != nil { |
| 281 |
for i := 0; i < roots.count; i++ { |
| 282 |
pr := roots.roots[i] |
| 283 |
addr := uintptr(pr.decl) |
| 284 |
if cgoInRange(p, addr, addr+pr.size) { |
| 285 |
return true |
| 286 |
} |
| 287 |
} |
| 288 |
roots = roots.next |
| 289 |
} |
| 290 |
|
| 291 |
return false |
| 292 |
} |
| 293 |
|
| 294 |
// cgoInRange reports whether p is between start and end. |
| 295 |
//go:nosplit |
| 296 |
//go:nowritebarrierrec |
| 297 |
func cgoInRange(p unsafe.Pointer, start, end uintptr) bool { |
| 298 |
return start <= uintptr(p) && uintptr(p) < end |
| 299 |
} |
| 300 |
|
| 301 |
// cgoCheckResult is called to check the result parameter of an |
| 302 |
// exported Go function. It panics if the result is or contains a Go |
| 303 |
// pointer. |
| 304 |
func cgoCheckResult(val any) { |
| 305 |
if debug.cgocheck == 0 { |
| 306 |
return |
| 307 |
} |
| 308 |
|
| 309 |
ep := efaceOf(&val) |
| 310 |
t := ep._type |
| 311 |
cgoCheckArg(t, ep.data, t.kind&kindDirectIface == 0, false, cgoResultFail) |
| 312 |
} |