rootmap.mx raw

   1  package runtime
   2  
   3  import "unsafe"
   4  
   5  // Frame root maps.
   6  //
   7  // At every select the compiler stores the current value of each pointer-typed
   8  // variable in scope into a compiler-built array and passes that array here. The
   9  // array embeds its own map, so the runtime needs exactly one pointer:
  10  //
  11  //	word 0            count n
  12  //	word 1 + 3*i      kind of root i
  13  //	word 2 + 3*i      value offset of root i (from the value base)
  14  //	word 3 + 3*i      value width in words
  15  //	value base        the values (1 + 3*n)
  16  //
  17  // The compiler reloads the values after the select returns, so a relocation the
  18  // runtime performs during the park reaches the program. Slices and pointers are
  19  // traced; any other pointer-shaped variable is recorded as kind 9 so a
  20  // compaction refuses the frame instead of moving something it cannot see.
  21  //
  22  // The registry is a small TLS stack because selects nest: an arm's body may
  23  // select again, and the inner map must not be visible to the outer one.
  24  const (
  25  	rootKindSlice       = 1
  26  	rootKindPtr         = 2
  27  	rootKindUnsupported = 9
  28  
  29  	rootStackMax = 8
  30  )
  31  
  32  var (
  33  	rootDepth int32
  34  	rootStack [rootStackMax]uintptr
  35  
  36  	// The most recent map is kept after it leaves: the frame that built it is
  37  	// still alive, and a report or a test can read it.
  38  	lastRoots uintptr
  39  )
  40  
  41  // RootMapEnter publishes the roots of the select about to run.
  42  func RootMapEnter(roots unsafe.Pointer) {
  43  	p := uintptr(roots)
  44  	if p == 0 {
  45  		return
  46  	}
  47  	if rootDepth >= rootStackMax {
  48  		runtimePanic("select: root map nesting deeper than 8")
  49  	}
  50  	rootStack[rootDepth] = p
  51  	rootDepth++
  52  	lastRoots = p
  53  }
  54  
  55  // RootMapLeave retires the innermost map.
  56  func RootMapLeave() {
  57  	if rootDepth > 0 {
  58  		rootDepth--
  59  	}
  60  }
  61  
  62  // RootMapCurrent returns the roots of the running select, or zero when no
  63  // select's park is the current context.
  64  func RootMapCurrent() (roots unsafe.Pointer) {
  65  	if rootDepth == 0 {
  66  		return nil
  67  	}
  68  	return unsafe.Pointer(rootStack[rootDepth-1])
  69  }
  70  
  71  // RootDepth is how many select root maps are published on this domain. A
  72  // balanced program returns to zero after every select; a non-zero depth at an
  73  // event-loop yield means a map was published and never retired.
  74  func RootDepth() (n int32) { return rootDepth }
  75  
  76  // RootCurrentCount is the number of roots in the map currently published (zero
  77  // when no select is active). Unlike RootCount it does not read lastRoots, whose
  78  // backing alloca may already have been released.
  79  func RootCurrentCount() (n int32) {
  80  	p := uintptr(RootMapCurrent())
  81  	if p == 0 {
  82  		return 0
  83  	}
  84  	return int32(rootWord(p, 0))
  85  }
  86  
  87  // RootCurrentKind is the kind of root i in the currently published map.
  88  func RootCurrentKind(i int32) (k int32) {
  89  	p := uintptr(RootMapCurrent())
  90  	if p == 0 || i < 0 || i >= RootCurrentCount() {
  91  		return 0
  92  	}
  93  	return int32(rootWord(p, 1+3*i))
  94  }
  95  
  96  func rootWord(p uintptr, i int32) (v uintptr) {
  97  	return *(*uintptr)(unsafe.Pointer(p + uintptr(i)*unsafe.Sizeof(uintptr(0))))
  98  }
  99  
 100  func rootSetWord(p uintptr, i int32, v uintptr) {
 101  	*(*uintptr)(unsafe.Pointer(p + uintptr(i)*unsafe.Sizeof(uintptr(0)))) = v
 102  }
 103  
 104  // RootCount is the number of roots in the last map.
 105  func RootCount() (n int32) {
 106  	if lastRoots == 0 {
 107  		return 0
 108  	}
 109  	return int32(rootWord(lastRoots, 0))
 110  }
 111  
 112  // RootKind is the kind of root i in the last map.
 113  func RootKind(i int32) (k int32) {
 114  	if lastRoots == 0 || i < 0 || i >= RootCount() {
 115  		return 0
 116  	}
 117  	return int32(rootWord(lastRoots, 1+3*i))
 118  }
 119  
 120  // RootIndex returns the value word at position w of root i.
 121  func RootIndex(i, w int32) (v uintptr) {
 122  	n := RootCount()
 123  	if lastRoots == 0 || i < 0 || i >= n {
 124  		return 0
 125  	}
 126  	if w < 0 || w >= int32(rootWord(lastRoots, 3+3*i)) {
 127  		return 0
 128  	}
 129  	// The offset is from the value base, not from the array start.
 130  	base := int32(1) + 3*n
 131  	return rootWord(lastRoots, base+int32(rootWord(lastRoots, 2+3*i))+w)
 132  }
 133  
 134  // RootSetIndex writes the value word at position w of root i. The compiler's
 135  // reload after the select makes it visible to the program.
 136  func RootSetIndex(i, w int32, v uintptr) {
 137  	n := RootCount()
 138  	if lastRoots == 0 || i < 0 || i >= n {
 139  		return
 140  	}
 141  	if w < 0 || w >= int32(rootWord(lastRoots, 3+3*i)) {
 142  		return
 143  	}
 144  	base := int32(1) + 3*n
 145  	rootSetWord(lastRoots, base+int32(rootWord(lastRoots, 2+3*i))+w, v)
 146  }
 147  
 148  // RootMapRefuses reports whether the running select's frame holds a root the
 149  // compactor cannot trace: any kind other than a pointer-free slice or a
 150  // pointer. A map, channel, interface or function root could name any arena's
 151  // data, so a compaction refuses the whole frame rather than moving what it
 152  // cannot see. With no select running there is no frame to refuse.
 153  func RootMapRefuses() (ok bool) {
 154  	p := uintptr(RootMapCurrent())
 155  	if p == 0 {
 156  		return false
 157  	}
 158  	n := int32(rootWord(p, 0))
 159  	for i := int32(0); i < n; i++ {
 160  		k := int32(rootWord(p, 1+3*i))
 161  		if k != rootKindSlice && k != rootKindPtr {
 162  			return true
 163  		}
 164  	}
 165  	return false
 166  }
 167  
 168  // RootsCompactable reports whether every root is a kind the compactor can
 169  // trace: only slices and pointers in this version.
 170  func RootsCompactable() (ok bool) {
 171  	n := RootCount()
 172  	for i := int32(0); i < n; i++ {
 173  		k := RootKind(i)
 174  		if k != rootKindSlice && k != rootKindPtr {
 175  			return false
 176  		}
 177  	}
 178  	return n > 0
 179  }
 180