slice_cursor_zero_fill.mx raw

   1  // Test: a capacity-exact cursor slice filled by `push`, read back by index.
   2  //
   3  // This is the minimal form of the pattern that made the smesh signer unable to
   4  // log in: `o := []byte{:32}[:0]`, a 5-bits-per-character accumulator that
   5  // pushes one byte whenever it has 8 bits, then `result[i] = o[i]` over all 32.
   6  //
   7  // It lays the bytes down from BOTH ends so the failure is directional rather
   8  // than "everything is zero": hit[i] and hit[31-i] are set for each byte, and
   9  // either 32-byte output being wrong is a FAIL. If the cursor is filled from the
  10  // wrong end, or the tail is left zeroed, or the length is short by one, the
  11  // symmetric copies disagree.
  12  //
  13  // Run natively AND under -target wasm: the signer failure was only ever seen
  14  // under wasm, so the fixture is meaningful as a wasm target too. It must not
  15  // carry the wasm-skip marker. (This comment deliberately does not spell that
  16  // marker out: the fixture scanner greps the file for the literal directive, and
  17  // a mention inside a comment is indistinguishable from its use. Writing it here
  18  // once excluded this fixture from phase 5 silently, which is the same trap in
  19  // miniature.)
  20  package main
  21  
  22  func decode(input string, forward []byte, backward []byte) (count int32) {
  23  	data := []byte{:32}[:0]
  24  	acc := uint32(0)
  25  	bits := uint32(0)
  26  	for i := 0; i < len(input); i++ {
  27  		v := uint32(input[i]) & 31
  28  		acc = (acc << 5) | v
  29  		bits += 5
  30  		for bits >= 8 {
  31  			bits -= 8
  32  			data = push(data, byte(acc>>bits))
  33  		}
  34  	}
  35  	count = int32(len(data))
  36  	for i := 0; i < 32; i++ {
  37  		forward[i] = data[i]
  38  		backward[31-i] = data[i]
  39  	}
  40  	return count
  41  }
  42  
  43  // input is 52 five-bit characters: 11 whole bytes per 8 characters (5 bytes),
  44  // so 52 characters is exactly 32 bytes with 4 bits left over.
  45  func fixtureInput() (s string) {
  46  	b := []byte{:52}
  47  	for i := 0; i < 52; i++ {
  48  		b[i] = byte('a' + (i % 26))
  49  	}
  50  	return string(b)
  51  }
  52  
  53  func main() {
  54  	// Termination: a batch program must still be stoppable, so the entry
  55  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  56  	select {
  57  	case <-shutdown:
  58  		return
  59  	default:
  60  	}
  61  
  62  	fwd := []byte{:32}
  63  	bwd := []byte{:32}
  64  	n := decode(fixtureInput(), fwd, bwd)
  65  	if n != 32 {
  66  		println("FAIL: cursor length", n, "want 32")
  67  		return
  68  	}
  69  	for i := 0; i < 32; i++ {
  70  		if fwd[i] != bwd[31-i] {
  71  			println("FAIL: forward/backward disagree at", i)
  72  			return
  73  		}
  74  	}
  75  	// The first byte of the accumulator for "abcde..." is non-zero; a cursor
  76  	// that silently zeroed would pass the symmetry check alone.
  77  	if fwd[0] == 0 && fwd[1] == 0 && fwd[2] == 0 && fwd[31] == 0 {
  78  		println("FAIL: cursor read back as all zero")
  79  		return
  80  	}
  81  	// The tail is data, not padding: position 31 must be written too.
  82  	if bwd[0] == 0 {
  83  		println("FAIL: tail byte never written")
  84  		return
  85  	}
  86  	println("PASS: slice cursor zero fill")
  87  }
  88