lib.mx raw

   1  package lib
   2  
   3  // Regression: the compiler's descriptor layout computed the size of an array as
   4  // `(n-1)*align(elem) + size(elem)`, which is only valid when an element's size
   5  // equals its alignment. For `[3][]T` (24-byte element, 8-byte alignment) it
   6  // reported 40 instead of 72, so every field after such an array was 32 bytes
   7  // off in the type descriptor the codec relocates values with.
   8  //
   9  // zstd's Reader is the shape that found it: `seqTables [3][]fseBaselineEntry`
  10  // sits twelve fields before the checksum state, so a decoded frame's xxhash
  11  // header was read from the wrong slot and the decoder crashed in memset.
  12  //
  13  // The fixture is the same shape without the decoder: a struct with an array of
  14  // slices followed by scalars, arrays and a nested struct that itself holds
  15  // slices, built in a frame and returned, so the codec relocates all of it.
  16  type Inner struct {
  17  	len uint64
  18  	v   []uint64
  19  	buf []byte
  20  	cnt int32
  21  }
  22  
  23  type Holder struct {
  24  	tables [3][]uint16
  25  	bits   [3]uint8
  26  	more   [3][]uint16
  27  	sl     []byte
  28  	inner  Inner
  29  	tail   int32
  30  }
  31  
  32  func NewHolder() (h *Holder) {
  33  	h = &Holder{}
  34  	for i := int32(0); i < 3; i++ {
  35  		h.tables[i] = []uint16{:4}
  36  		h.tables[i][0] = uint16(0x1000 + i)
  37  		h.tables[i][3] = uint16(0x2000 + i)
  38  		h.bits[i] = uint8(i + 1)
  39  		h.more[i] = []uint16{:2}
  40  		h.more[i][1] = uint16(0x3000 + i)
  41  	}
  42  	h.sl = []byte{:8}
  43  	h.sl[7] = 0xAB
  44  	h.inner.len = 9
  45  	h.inner.v = []uint64{:4}
  46  	h.inner.v[3] = 0xDEAD
  47  	h.inner.buf = []byte{:32}
  48  	h.inner.buf[31] = 0x55
  49  	h.inner.cnt = 3
  50  	h.tail = 77
  51  	return h
  52  }
  53  
  54  func Check(h *Holder) (msg []byte) {
  55  	for i := int32(0); i < 3; i++ {
  56  		if len(h.tables[i]) != 4 || h.tables[i][0] != uint16(0x1000+i) || h.tables[i][3] != uint16(0x2000+i) {
  57  			return "tables wrong"
  58  		}
  59  		if h.bits[i] != uint8(i+1) {
  60  			return "bits wrong"
  61  		}
  62  		if len(h.more[i]) != 2 || h.more[i][1] != uint16(0x3000+i) {
  63  			return "more wrong"
  64  		}
  65  	}
  66  	if len(h.sl) != 8 || h.sl[7] != 0xAB {
  67  		return "sl wrong"
  68  	}
  69  	if h.inner.len != 9 || h.tail != 77 || h.inner.cnt != 3 {
  70  		return "scalars wrong"
  71  	}
  72  	if len(h.inner.v) != 4 || h.inner.v[3] != 0xDEAD {
  73  		return "inner.v wrong"
  74  	}
  75  	if len(h.inner.buf) != 32 || h.inner.buf[31] != 0x55 {
  76  		return "inner.buf wrong"
  77  	}
  78  	return "ok"
  79  }
  80