| 1 |
// Copyright 2015 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 |
// Code to check that pointer writes follow the cgo rules. |
| 6 |
// These functions are invoked via the write barrier when debug.cgocheck > 1. |
| 7 |
|
| 8 |
package runtime |
| 9 |
|
| 10 |
import ( |
| 11 |
"internal/goarch" |
| 12 |
"unsafe" |
| 13 |
) |
| 14 |
|
| 15 |
const cgoWriteBarrierFail = "Go pointer stored into non-Go memory" |
| 16 |
|
| 17 |
// cgoCheckWriteBarrier is called whenever a pointer is stored into memory. |
| 18 |
// It throws if the program is storing a Go pointer into non-Go memory. |
| 19 |
// |
| 20 |
// This is called from the write barrier, so its entire call tree must |
| 21 |
// be nosplit. |
| 22 |
// |
| 23 |
//go:nosplit |
| 24 |
//go:nowritebarrier |
| 25 |
func cgoCheckWriteBarrier(dst *uintptr, src uintptr) { |
| 26 |
if !cgoIsGoPointer(unsafe.Pointer(src)) { |
| 27 |
return |
| 28 |
} |
| 29 |
if cgoIsGoPointer(unsafe.Pointer(dst)) { |
| 30 |
return |
| 31 |
} |
| 32 |
|
| 33 |
// If we are running on the system stack then dst might be an |
| 34 |
// address on the stack, which is OK. |
| 35 |
g := getg() |
| 36 |
if g == g.m.g0 || g == g.m.gsignal { |
| 37 |
return |
| 38 |
} |
| 39 |
|
| 40 |
// Allocating memory can write to various mfixalloc structs |
| 41 |
// that look like they are non-Go memory. |
| 42 |
if g.m.mallocing != 0 { |
| 43 |
return |
| 44 |
} |
| 45 |
|
| 46 |
// It's OK if writing to memory allocated by persistentalloc. |
| 47 |
// Do this check last because it is more expensive and rarely true. |
| 48 |
// If it is false the expense doesn't matter since we are crashing. |
| 49 |
if inPersistentAlloc(uintptr(unsafe.Pointer(dst))) { |
| 50 |
return |
| 51 |
} |
| 52 |
|
| 53 |
systemstack(func() { |
| 54 |
println("write of Go pointer", hex(src), "to non-Go memory", hex(uintptr(unsafe.Pointer(dst)))) |
| 55 |
throw(cgoWriteBarrierFail) |
| 56 |
}) |
| 57 |
} |
| 58 |
|
| 59 |
// cgoCheckMemmove is called when moving a block of memory. |
| 60 |
// dst and src point off bytes into the value to copy. |
| 61 |
// size is the number of bytes to copy. |
| 62 |
// It throws if the program is copying a block that contains a Go pointer |
| 63 |
// into non-Go memory. |
| 64 |
//go:nosplit |
| 65 |
//go:nowritebarrier |
| 66 |
func cgoCheckMemmove(typ *_type, dst, src unsafe.Pointer, off, size uintptr) { |
| 67 |
if typ.ptrdata == 0 { |
| 68 |
return |
| 69 |
} |
| 70 |
if !cgoIsGoPointer(src) { |
| 71 |
return |
| 72 |
} |
| 73 |
if cgoIsGoPointer(dst) { |
| 74 |
return |
| 75 |
} |
| 76 |
cgoCheckTypedBlock(typ, src, off, size) |
| 77 |
} |
| 78 |
|
| 79 |
// cgoCheckSliceCopy is called when copying n elements of a slice. |
| 80 |
// src and dst are pointers to the first element of the slice. |
| 81 |
// typ is the element type of the slice. |
| 82 |
// It throws if the program is copying slice elements that contain Go pointers |
| 83 |
// into non-Go memory. |
| 84 |
//go:nosplit |
| 85 |
//go:nowritebarrier |
| 86 |
func cgoCheckSliceCopy(typ *_type, dst, src unsafe.Pointer, n int) { |
| 87 |
if typ.ptrdata == 0 { |
| 88 |
return |
| 89 |
} |
| 90 |
if !cgoIsGoPointer(src) { |
| 91 |
return |
| 92 |
} |
| 93 |
if cgoIsGoPointer(dst) { |
| 94 |
return |
| 95 |
} |
| 96 |
p := src |
| 97 |
for i := 0; i < n; i++ { |
| 98 |
cgoCheckTypedBlock(typ, p, 0, typ.size) |
| 99 |
p = add(p, typ.size) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
// cgoCheckTypedBlock checks the block of memory at src, for up to size bytes, |
| 104 |
// and throws if it finds a Go pointer. The type of the memory is typ, |
| 105 |
// and src is off bytes into that type. |
| 106 |
//go:nosplit |
| 107 |
//go:nowritebarrier |
| 108 |
func cgoCheckTypedBlock(typ *_type, src unsafe.Pointer, off, size uintptr) { |
| 109 |
// Anything past typ.ptrdata is not a pointer. |
| 110 |
if typ.ptrdata <= off { |
| 111 |
return |
| 112 |
} |
| 113 |
if ptrdataSize := typ.ptrdata - off; size > ptrdataSize { |
| 114 |
size = ptrdataSize |
| 115 |
} |
| 116 |
|
| 117 |
if typ.kind&kindGCProg == 0 { |
| 118 |
cgoCheckBits(src, typ.gcdata, off, size) |
| 119 |
return |
| 120 |
} |
| 121 |
|
| 122 |
// The type has a GC program. Try to find GC bits somewhere else. |
| 123 |
roots := gcRoots |
| 124 |
for roots != nil { |
| 125 |
for i := 0; i < roots.count; i++ { |
| 126 |
pr := roots.roots[i] |
| 127 |
addr := uintptr(pr.decl) |
| 128 |
if cgoInRange(src, addr, addr+pr.size) { |
| 129 |
doff := uintptr(src) - addr |
| 130 |
cgoCheckBits(add(src, -doff), pr.gcdata, off+doff, size) |
| 131 |
return |
| 132 |
} |
| 133 |
} |
| 134 |
roots = roots.next |
| 135 |
} |
| 136 |
|
| 137 |
s := spanOfUnchecked(uintptr(src)) |
| 138 |
if s.state.get() == mSpanManual { |
| 139 |
// There are no heap bits for value stored on the stack. |
| 140 |
// For a channel receive src might be on the stack of some |
| 141 |
// other goroutine, so we can't unwind the stack even if |
| 142 |
// we wanted to. |
| 143 |
// We can't expand the GC program without extra storage |
| 144 |
// space we can't easily get. |
| 145 |
// Fortunately we have the type information. |
| 146 |
systemstack(func() { |
| 147 |
cgoCheckUsingType(typ, src, off, size) |
| 148 |
}) |
| 149 |
return |
| 150 |
} |
| 151 |
|
| 152 |
// src must be in the regular heap. |
| 153 |
|
| 154 |
hbits := heapBitsForAddr(uintptr(src)) |
| 155 |
for i := uintptr(0); i < off+size; i += goarch.PtrSize { |
| 156 |
bits := hbits.bits() |
| 157 |
if i >= off && bits&bitPointer != 0 { |
| 158 |
v := *(*unsafe.Pointer)(add(src, i)) |
| 159 |
if cgoIsGoPointer(v) { |
| 160 |
throw(cgoWriteBarrierFail) |
| 161 |
} |
| 162 |
} |
| 163 |
hbits = hbits.next() |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
// cgoCheckBits checks the block of memory at src, for up to size |
| 168 |
// bytes, and throws if it finds a Go pointer. The gcbits mark each |
| 169 |
// pointer value. The src pointer is off bytes into the gcbits. |
| 170 |
//go:nosplit |
| 171 |
//go:nowritebarrier |
| 172 |
func cgoCheckBits(src unsafe.Pointer, gcbits *byte, off, size uintptr) { |
| 173 |
skipMask := off / goarch.PtrSize / 8 |
| 174 |
skipBytes := skipMask * goarch.PtrSize * 8 |
| 175 |
ptrmask := addb(gcbits, skipMask) |
| 176 |
src = add(src, skipBytes) |
| 177 |
off -= skipBytes |
| 178 |
size += off |
| 179 |
var bits uint32 |
| 180 |
for i := uintptr(0); i < size; i += goarch.PtrSize { |
| 181 |
if i&(goarch.PtrSize*8-1) == 0 { |
| 182 |
bits = uint32(*ptrmask) |
| 183 |
ptrmask = addb(ptrmask, 1) |
| 184 |
} else { |
| 185 |
bits >>= 1 |
| 186 |
} |
| 187 |
if off > 0 { |
| 188 |
off -= goarch.PtrSize |
| 189 |
} else { |
| 190 |
if bits&1 != 0 { |
| 191 |
v := *(*unsafe.Pointer)(add(src, i)) |
| 192 |
if cgoIsGoPointer(v) { |
| 193 |
throw(cgoWriteBarrierFail) |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
// cgoCheckUsingType is like cgoCheckTypedBlock, but is a last ditch |
| 201 |
// fall back to look for pointers in src using the type information. |
| 202 |
// We only use this when looking at a value on the stack when the type |
| 203 |
// uses a GC program, because otherwise it's more efficient to use the |
| 204 |
// GC bits. This is called on the system stack. |
| 205 |
//go:nowritebarrier |
| 206 |
//go:systemstack |
| 207 |
func cgoCheckUsingType(typ *_type, src unsafe.Pointer, off, size uintptr) { |
| 208 |
if typ.ptrdata == 0 { |
| 209 |
return |
| 210 |
} |
| 211 |
|
| 212 |
// Anything past typ.ptrdata is not a pointer. |
| 213 |
if typ.ptrdata <= off { |
| 214 |
return |
| 215 |
} |
| 216 |
if ptrdataSize := typ.ptrdata - off; size > ptrdataSize { |
| 217 |
size = ptrdataSize |
| 218 |
} |
| 219 |
|
| 220 |
if typ.kind&kindGCProg == 0 { |
| 221 |
cgoCheckBits(src, typ.gcdata, off, size) |
| 222 |
return |
| 223 |
} |
| 224 |
switch typ.kind & kindMask { |
| 225 |
default: |
| 226 |
throw("can't happen") |
| 227 |
case kindArray: |
| 228 |
at := (*arraytype)(unsafe.Pointer(typ)) |
| 229 |
for i := uintptr(0); i < at.len; i++ { |
| 230 |
if off < at.elem.size { |
| 231 |
cgoCheckUsingType(at.elem, src, off, size) |
| 232 |
} |
| 233 |
src = add(src, at.elem.size) |
| 234 |
skipped := off |
| 235 |
if skipped > at.elem.size { |
| 236 |
skipped = at.elem.size |
| 237 |
} |
| 238 |
checked := at.elem.size - skipped |
| 239 |
off -= skipped |
| 240 |
if size <= checked { |
| 241 |
return |
| 242 |
} |
| 243 |
size -= checked |
| 244 |
} |
| 245 |
case kindStruct: |
| 246 |
st := (*structtype)(unsafe.Pointer(typ)) |
| 247 |
for _, f := range st.fields { |
| 248 |
if off < f.typ.size { |
| 249 |
cgoCheckUsingType(f.typ, src, off, size) |
| 250 |
} |
| 251 |
src = add(src, f.typ.size) |
| 252 |
skipped := off |
| 253 |
if skipped > f.typ.size { |
| 254 |
skipped = f.typ.size |
| 255 |
} |
| 256 |
checked := f.typ.size - skipped |
| 257 |
off -= skipped |
| 258 |
if size <= checked { |
| 259 |
return |
| 260 |
} |
| 261 |
size -= checked |
| 262 |
} |
| 263 |
} |
| 264 |
} |