github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/src/runtime/cgocheck.go (about)

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