addr_of_composite_lit.mx raw

   1  // &T{...} must build the literal INTO the allocation the address denotes.
   2  //
   3  // Falling through to the generic `&` path built the whole aggregate as an SSA
   4  // VALUE and then copied it into a fresh arena allocation. For an aggregate the
   5  // size of tracev1's Event (64 bytes) times eventsBucketSize (524288) that is a
   6  // 32 MiB alloca, a 32 MiB load and a 32 MiB store - and clang's DAG combiner
   7  // segfaults in SelectionDAG::ReplaceAllUsesWith on it (tracev1's
   8  // Events.grow: `push(l.buckets, &[eventsBucketSize]Event{})`). The array below
   9  // is deliberately that size and shape, so a regression crashes the compiler
  10  // rather than merely being wasteful.
  11  package main
  12  
  13  type event struct {
  14  	Ts    int64
  15  	G     uint64
  16  	Args  [4]uint64
  17  	StkID uint32
  18  	P     int32
  19  	Type  uint8
  20  }
  21  
  22  type holder struct {
  23  	p *event
  24  	n int32
  25  	s []byte
  26  }
  27  
  28  const bucketSize = 524288
  29  
  30  func main() {
  31  	// Termination: a batch program must still be stoppable, so the entry
  32  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  33  	select {
  34  	case <-shutdown:
  35  		return
  36  	default:
  37  	}
  38  
  39  	// A partial literal leaves the unset fields at the zero value the allocator
  40  	// provides (runtime.alloc memzeros).
  41  	p := &event{Ts: 7}
  42  	if p.Ts != 7 || p.G != 0 || p.Args[3] != 0 || p.StkID != 0 || p.P != 0 || p.Type != 0 {
  43  		panic("partial &T{...} not zeroed")
  44  	}
  45  	// A fully covering literal keeps every field.
  46  	q := &event{Ts: 1, G: 2, Args: [4]uint64{3, 4, 5, 6}, StkID: 7, P: 8, Type: 9}
  47  	if q.Ts != 1 || q.G != 2 || q.Args[0] != 3 || q.Args[3] != 6 || q.StkID != 7 || q.P != 8 || q.Type != 9 {
  48  		panic("&T{...} fields wrong")
  49  	}
  50  	// Nested: an address-taken literal inside another address-taken literal.
  51  	h := &holder{p: &event{Ts: 11}, n: 4}
  52  	if h.p == nil || h.p.Ts != 11 || h.n != 4 || h.s != nil {
  53  		panic("nested &T{...} wrong")
  54  	}
  55  	// Distinct literals must not share storage.
  56  	if h.p == q {
  57  		panic("distinct &T{...} share an address")
  58  	}
  59  	// The large bucket: 32 MiB, zeroed and writable.
  60  	arr := &[bucketSize]event{}
  61  	if arr[0].Ts != 0 || arr[bucketSize-1].Ts != 0 {
  62  		panic("large &[N]T{} not zeroed")
  63  	}
  64  	arr[bucketSize-1].Ts = 42
  65  	if arr[bucketSize-1].Ts != 42 {
  66  		panic("large &[N]T{} not writable")
  67  	}
  68  }
  69