/* A //:linkdata binding has a fixed length, and the length-changing builtins refuse it. The storage is an external symbol, so the slice header is built with len equal to the declared length and cap all-ones, which cap() reads as -1. push, pop and resize all check for that sentinel and die with "slice length is fixed: the storage is not ours" rather than growing, shrinking, or - the trap push had before - accepting the write because an all-ones capacity looks large enough. A panic cannot be asserted inside a fixture (there is no recover), so this file pins what can be: the header the compiler builds and the fact that ordinary slices keep working. The three panics were measured with throwaway probes against both compilers. */ package main import ( "crypto/internal/fips140/check" "os" ) //:linkdata 16 fixturesymbol var local []byte func checkInt(name string, got, want int32) { if got != want { println("FAIL: " | name | ": got ", got, " want ", want) os.Exit(1) } } func main() { // Termination: a batch program must still be stoppable, so the entry // handles the channel the runtime closes on SIGINT/SIGTERM. select { case <-shutdown: return default: } // The binding: len is the declared length, and cap reports the SAME length. // The all-ones capacity is internal (push/pop/resize see it and panic); // exposing it broke the 0 <= len <= cap invariant every consumer assumes. checkInt("check.Linkinfo len", int32(len(check.Linkinfo)), 48) checkInt("check.Linkinfo cap", int32(cap(check.Linkinfo)), 48) checkInt("local len", int32(len(local)), 16) checkInt("local cap", int32(cap(local)), 16) // A subslice is a copy into normal storage whose cap is the RANGE length, // so the copy is an ordinary slice and push/pop work on it: "copy it first". // The same applies to a subslice or clone of a binding - the bytes are // copied out and the result has cap == len - but the fixture's symbols are // undefined at link time (the linker leaves them null), so the copy is // demonstrated on a normal slice with the same shape. base := []byte{:4} sub := base[1:3] checkInt("sub len", int32(len(sub)), 2) checkInt("sub cap", int32(cap(sub)), 2) sub = push(sub, 9) checkInt("sub push len", int32(len(sub)), 3) checkInt("sub push cap", int32(cap(sub)), 4) // Ordinary slices are unaffected: the sentinel is -1, never a real capacity. s := []int32{:0:2} s = push(s, 10, 20, 30) checkInt("push len", int32(len(s)), 3) checkInt("push cap", int32(cap(s)), 4) checkInt("push value", s[2], 30) s = resize(s, 2) checkInt("resize len", int32(len(s)), 2) checkInt("resize cap", int32(cap(s)), 4) x := s[len(s)-1] pop(s) checkInt("pop value", x, 20) checkInt("pop len", int32(len(s)), 1) // Lazy init from a zero-capacity slice still works. var z []int32 z = push(z, 7) checkInt("lazy len", int32(len(z)), 1) checkInt("lazy value", z[0], 7) println("ok") }