Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
stack.go101 linesDownload Raw Back to test
1// run2 3// Copyright 2009 The Go Authors. All rights reserved.4// Use of this source code is governed by a BSD-style5// license that can be found in the LICENSE file.6 7// Test stack splitting code.8// Try to tickle stack splitting bugs by doing9// go, defer, and closure calls at different stack depths.10 11package main12 13type T [20]int14 15func g(c chan int, t T) {16	s := 017	for i := 0; i < len(t); i++ {18		s += t[i]19	}20	c <- s21}22 23func d(t T) {24	s := 025	for i := 0; i < len(t); i++ {26		s += t[i]27	}28	if s != len(t) {29		println("bad defer", s)30		panic("fail")31	}32}33 34func f0() {35	// likely to make a new stack for f0,36	// because the call to f1 puts 3000 bytes37	// in our frame.38	f1()39}40 41func f1() [3000]byte {42	// likely to make a new stack for f1,43	// because 3000 bytes were used by f044	// and we need 3000 more for the call45	// to f2.  if the call to morestack in f146	// does not pass the frame size, the new47	// stack (default size 5k) will not be big48	// enough for the frame, and the morestack49	// check in f2 will die, if we get that far 50	// without faulting.51	f2()52	return [3000]byte{}53}54 55func f2() [3000]byte {56	// just take up space57	return [3000]byte{}58}59 60var c = make(chan int)61var t T62var b = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}63 64func recur(n int) {65	ss := string(b)66	if len(ss) != len(b) {67		panic("bad []byte -> string")68	}69	go g(c, t)70	f0()71	s := <-c72	if s != len(t) {73		println("bad go", s)74		panic("fail")75	}76	f := func(t T) int {77		s := 078		for i := 0; i < len(t); i++ {79			s += t[i]80		}81		s += n82		return s83	}84	s = f(t)85	if s != len(t)+n {86		println("bad func", s, "at level", n)87		panic("fail")88	}89	if n > 0 {90		recur(n - 1)91	}92	defer d(t)93}94 95func main() {96	for i := 0; i < len(t); i++ {97		t[i] = 198	}99	recur(8000)100}101