linkdata_fixed_length.mx raw

   1  /*
   2  A //:linkdata binding has a fixed length, and the length-changing builtins
   3  refuse it.
   4  
   5  The storage is an external symbol, so the slice header is built with len equal
   6  to the declared length and cap all-ones, which cap() reads as -1. push, pop and
   7  resize all check for that sentinel and die with "slice length is fixed: the
   8  storage is not ours" rather than growing, shrinking, or - the trap push had
   9  before - accepting the write because an all-ones capacity looks large enough.
  10  
  11  A panic cannot be asserted inside a fixture (there is no recover), so this file
  12  pins what can be: the header the compiler builds and the fact that ordinary
  13  slices keep working. The three panics were measured with throwaway probes
  14  against both compilers.
  15  */
  16  package main
  17  
  18  import (
  19  	"crypto/internal/fips140/check"
  20  	"os"
  21  )
  22  
  23  //:linkdata 16 fixturesymbol
  24  var local []byte
  25  
  26  func checkInt(name string, got, want int32) {
  27  	if got != want {
  28  		println("FAIL: " | name | ": got ", got, " want ", want)
  29  		os.Exit(1)
  30  	}
  31  }
  32  
  33  func main() {
  34  	// Termination: a batch program must still be stoppable, so the entry
  35  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  36  	select {
  37  	case <-shutdown:
  38  		return
  39  	default:
  40  	}
  41  
  42  	// The binding: len is the declared length, and cap reports the SAME length.
  43  	// The all-ones capacity is internal (push/pop/resize see it and panic);
  44  	// exposing it broke the 0 <= len <= cap invariant every consumer assumes.
  45  	checkInt("check.Linkinfo len", int32(len(check.Linkinfo)), 48)
  46  	checkInt("check.Linkinfo cap", int32(cap(check.Linkinfo)), 48)
  47  	checkInt("local len", int32(len(local)), 16)
  48  	checkInt("local cap", int32(cap(local)), 16)
  49  
  50  	// A subslice is a copy into normal storage whose cap is the RANGE length,
  51  	// so the copy is an ordinary slice and push/pop work on it: "copy it first".
  52  	// The same applies to a subslice or clone of a binding - the bytes are
  53  	// copied out and the result has cap == len - but the fixture's symbols are
  54  	// undefined at link time (the linker leaves them null), so the copy is
  55  	// demonstrated on a normal slice with the same shape.
  56  	base := []byte{:4}
  57  	sub := base[1:3]
  58  	checkInt("sub len", int32(len(sub)), 2)
  59  	checkInt("sub cap", int32(cap(sub)), 2)
  60  	sub = push(sub, 9)
  61  	checkInt("sub push len", int32(len(sub)), 3)
  62  	checkInt("sub push cap", int32(cap(sub)), 4)
  63  
  64  	// Ordinary slices are unaffected: the sentinel is -1, never a real capacity.
  65  	s := []int32{:0:2}
  66  	s = push(s, 10, 20, 30)
  67  	checkInt("push len", int32(len(s)), 3)
  68  	checkInt("push cap", int32(cap(s)), 4)
  69  	checkInt("push value", s[2], 30)
  70  	s = resize(s, 2)
  71  	checkInt("resize len", int32(len(s)), 2)
  72  	checkInt("resize cap", int32(cap(s)), 4)
  73  	x := s[len(s)-1]
  74  	pop(s)
  75  	checkInt("pop value", x, 20)
  76  	checkInt("pop len", int32(len(s)), 1)
  77  
  78  	// Lazy init from a zero-capacity slice still works.
  79  	var z []int32
  80  	z = push(z, 7)
  81  	checkInt("lazy len", int32(len(z)), 1)
  82  	checkInt("lazy value", z[0], 7)
  83  
  84  	println("ok")
  85  }
  86