rootmap.mx raw

   1  // A select publishes the frame's pointer roots for the whole park: a slice is
   2  // three words (data, len, cap), a pointer one word, and any other
   3  // pointer-shaped local (map, channel, interface, function) is recorded as kind
   4  // 9 so a compaction refuses the frame instead of moving memory it cannot see.
   5  // The legacy loader keeps those slots (lift is skipped for them) and the
   6  // stage4 builder has them already; both publish the same map.
   7  package main
   8  
   9  import (
  10  	"runtime"
  11  	"unsafe"
  12  )
  13  
  14  var ch chan int32
  15  
  16  // refuseFrame selects with a map local in scope: its kind-9 root makes the
  17  // frame un-compactable, and the runtime must report that.
  18  func refuseFrame() (ok bool) {
  19  	m := map[int32]int32{}
  20  	m[1] = 2
  21  	select {
  22  	case <-ch:
  23  	default:
  24  	}
  25  	return runtime.RootCount() > 0 && !runtime.RootsCompactable()
  26  }
  27  
  28  func main() {
  29  	select {
  30  	case <-shutdown:
  31  		return
  32  	default:
  33  	}
  34  	b := []int32{10, 20, 30}
  35  	q := &b[1]
  36  	select {
  37  	case v := <-ch:
  38  		println(v)
  39  	default:
  40  	}
  41  	if runtime.RootCount() != 2 || !runtime.RootsCompactable() {
  42  		panic("frame roots not compactable")
  43  	}
  44  	seenSlice := false
  45  	seenPtr := false
  46  	for i := int32(0); i < runtime.RootCount(); i++ {
  47  		k := runtime.RootKind(i)
  48  		if k == 1 {
  49  			seenSlice = true
  50  			if runtime.RootIndex(i, 0) != uintptr(unsafe.Pointer(&b[0])) {
  51  				panic("slice data pointer mismatch")
  52  			}
  53  			if int32(runtime.RootIndex(i, 1)) != len(b) {
  54  				panic("slice length mismatch")
  55  			}
  56  		}
  57  		if k == 2 {
  58  			seenPtr = true
  59  			if runtime.RootIndex(i, 0) != uintptr(unsafe.Pointer(q)) {
  60  				panic("pointer root mismatch")
  61  			}
  62  		}
  63  	}
  64  	if !seenSlice || !seenPtr {
  65  		panic("missing roots")
  66  	}
  67  	if !refuseFrame() {
  68  		panic("kind-9 root not refused")
  69  	}
  70  	println("rootmap ok")
  71  }
  72