offsetframe.mx raw

   1  package runtime
   2  
   3  // Offset frames: a self-contained copy of a value in one buffer where every
   4  // pointer is an offset from the frame base instead of an address. Host-endian
   5  // by construction; the wire codec stays the fixed little-endian format for
   6  // pipes and sockets. Eligible values hold no pointers/maps/interfaces of their
   7  // own: a struct of scalars and string/[]byte fields, or a bare string/[]byte.
   8  //
   9  // This module is not yet wired into the channel dispatch: the encoder is
  10  // verified (a probe logs n=45 N=7 blen=5 for Msg{N:7, Body:[]byte("hello")}),
  11  // the receive side is waiting on the endpoint-resolution question recorded in
  12  // docs/the work log round 384. The guards below are the durable part: a
  13  // descriptor of the wrong kind is a loud error, never a garbage read.
  14  
  15  import "unsafe"
  16  
  17  const offsetFrameMagic = uint32(0x4d584f46)
  18  
  19  // offsetDbgEncode/Decode count calls so a log can tell "never entered" from
  20  // "entered and rejected" without a rebuild.
  21  var offsetDbgEncode uint64
  22  var offsetDbgDecode uint64
  23  var offsetDbgReject uint64
  24  
  25  // OffsetDebugReport prints the counters (called by a probe on either side).
  26  func OffsetDebugReport() {
  27  	printstring("offset: encode=")
  28  	printuint64(offsetDbgEncode)
  29  	printstring(" decode=")
  30  	printuint64(offsetDbgDecode)
  31  	printstring(" reject=")
  32  	printuint64(offsetDbgReject)
  33  	printstring("\n")
  34  }
  35  
  36  func offsetBasicKind(k Kind) (ok bool) {
  37  	switch k {
  38  	case Bool, Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32,
  39  		Uint64, Uintptr, Float32, Float64, Complex64, Complex128:
  40  		return true
  41  	}
  42  	return false
  43  }
  44  
  45  // offsetStruct resolves a struct descriptor and asserts its kind. A named
  46  // descriptor cast directly gave numField=0 and a random size; the guard makes
  47  // that a loud error on both the encode and the decode path.
  48  func offsetStruct(td *RawType, what string) (st *rtStructType) {
  49  	u := td.Underlying()
  50  	if u == nil || u.Kind() != KindStruct {
  51  		offsetDbgReject++
  52  		printstring("offset frame: ")
  53  		printstring(what)
  54  		printstring(" is not a struct descriptor\n")
  55  		runtimePanic("offset frame: wrong descriptor kind")
  56  	}
  57  	return (*rtStructType)(unsafe.Pointer(u))
  58  }
  59  
  60  func offsetBytes(td *RawType, what string) {
  61  	if td.Kind() != KindBytes {
  62  		offsetDbgReject++
  63  		printstring("offset frame: ")
  64  		printstring(what)
  65  		printstring(" is not a bytes descriptor\n")
  66  		runtimePanic("offset frame: wrong descriptor kind")
  67  	}
  68  }
  69  
  70  // offsetField returns field i's type and byte offset. The offset is a uvarint
  71  // in the field's data blob (retFieldOffsetAt decodes it), never the blob
  72  // pointer.
  73  func offsetField(st *rtStructType, i int32) (ft *RawType, off uintptr) {
  74  	fd := (*rtStructFieldDesc)(unsafe.Add(unsafe.Pointer(&st.fields[0]), uintptr(i)*unsafe.Sizeof(rtStructFieldDesc{})))
  75  	return fd.fieldType, retFieldOffsetAt(st, i)
  76  }
  77  
  78  func offsetFrameEligible(td *RawType) (ok bool) {
  79  	if td == nil {
  80  		return false
  81  	}
  82  	switch td.Kind() {
  83  	case KindBytes:
  84  		return true
  85  	case KindStruct:
  86  		st := offsetStruct(td, "element")
  87  		for i := int32(0); i < int32(st.numField); i++ {
  88  			ft, _ := offsetField(st, i)
  89  			k := ft.Kind()
  90  			if k != KindBytes && !offsetBasicKind(k) {
  91  				return false
  92  			}
  93  		}
  94  		return true
  95  	}
  96  	return false
  97  }
  98  
  99  func offsetPut32(b []byte, off uintptr, v uint32) {
 100  	*(*uint32)(unsafe.Pointer(&b[off])) = v
 101  }
 102  
 103  func offsetGet32(b []byte, off uintptr) (v uint32) {
 104  	return *(*uint32)(unsafe.Pointer(&b[off]))
 105  }
 106  
 107  func offsetFrameSize(v unsafe.Pointer, td *RawType) (n uintptr) {
 108  	n = 4 + td.Size()
 109  	switch td.Kind() {
 110  	case KindBytes:
 111  		offsetBytes(td, "element")
 112  		n += (*[3]uintptr)(v)[1]
 113  	case KindStruct:
 114  		st := offsetStruct(td, "element")
 115  		for i := int32(0); i < int32(st.numField); i++ {
 116  			ft, off := offsetField(st, i)
 117  			if ft.Kind() != KindBytes {
 118  				continue
 119  			}
 120  			n += (*[3]uintptr)(unsafe.Pointer(uintptr(v) + off))[1]
 121  		}
 122  	}
 123  	return n
 124  }
 125  
 126  func offsetFrameEncode(v unsafe.Pointer, td *RawType) (frame []byte, ok bool) {
 127  	offsetDbgEncode++
 128  	if !offsetFrameEligible(td) || v == nil {
 129  		offsetDbgReject++
 130  		return nil, false
 131  	}
 132  	n := offsetFrameSize(v, td)
 133  	if n == 0 || n > 0xffffffff {
 134  		offsetDbgReject++
 135  		return nil, false
 136  	}
 137  	bp := alloc(n, nil, nil)
 138  	memzero(bp, n)
 139  	frame = unsafe.Slice((*byte)(bp), n)
 140  	offsetPut32(frame, 0, offsetFrameMagic)
 141  	offsetPut32(frame, 4, uint32(n))
 142  	
 143  	memcpy(unsafe.Pointer(&frame[4]), v, td.Size())
 144  	data := uintptr(4) + td.Size()
 145  	switch td.Kind() {
 146  	case KindBytes:
 147  		srch := (*[3]uintptr)(v)
 148  		(*[3]uintptr)(unsafe.Pointer(&frame[4]))[0] = data
 149  		if srch[1] > 0 {
 150  			memcpy(unsafe.Pointer(&frame[data]), unsafe.Pointer(srch[0]), srch[1])
 151  		}
 152  	case KindStruct:
 153  		st := offsetStruct(td, "element")
 154  		for i := int32(0); i < int32(st.numField); i++ {
 155  			ft, off := offsetField(st, i)
 156  			if ft.Kind() != KindBytes {
 157  				continue
 158  			}
 159  			hdr := (*[3]uintptr)(unsafe.Pointer(&frame[4+off]))
 160  			srch := (*[3]uintptr)(unsafe.Pointer(uintptr(v) + off))
 161  			hdr[0] = data
 162  			if srch[1] > 0 {
 163  				memcpy(unsafe.Pointer(&frame[data]), unsafe.Pointer(srch[0]), srch[1])
 164  			}
 165  			data += srch[1]
 166  		}
 167  	}
 168  	return frame, true
 169  }
 170  
 171  func offsetFrameDecode(frame []byte, td *RawType, value unsafe.Pointer) (ok bool) {
 172  	offsetDbgDecode++
 173  	if !offsetFrameEligible(td) || len(frame) < 12 {
 174  		offsetDbgReject++
 175  		return false
 176  	}
 177  	if offsetGet32(frame, 0) != offsetFrameMagic {
 178  		offsetDbgReject++
 179  		return false
 180  	}
 181  	total := uintptr(len(frame))
 182  	bp := alloc(uintptr(total), nil, nil)
 183  	memcpy(bp, unsafe.Pointer(&frame[0]), uintptr(total))
 184  	base := uintptr(bp)
 185  	root := base + 4
 186  	switch td.Kind() {
 187  	case KindBytes:
 188  		hdr := (*[3]uintptr)(unsafe.Pointer(root))
 189  		if hdr[1] > 0 {
 190  			hdr[0] = base + hdr[0]
 191  		}
 192  	case KindStruct:
 193  		st := offsetStruct(td, "element")
 194  		for i := int32(0); i < int32(st.numField); i++ {
 195  			ft, off := offsetField(st, i)
 196  			if ft.Kind() != KindBytes {
 197  				continue
 198  			}
 199  			hdr := (*[3]uintptr)(unsafe.Pointer(root + off))
 200  			if hdr[1] > 0 {
 201  				hdr[0] = base + hdr[0]
 202  			}
 203  		}
 204  	}
 205  	memcpy(value, unsafe.Pointer(root), td.Size())
 206  	return true
 207  }
 208