1// run -gcflags=-d=maymorestack=main.mayMoreStack
2
3// Copyright 2021 The Go Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
7// Test the maymorestack testing hook by injecting a hook that counts
8// how many times it is called and checking that count.
9
10package main
11
12import "runtime"
13
14var count uint32
15
16//go:nosplit
17func mayMoreStack() {
18	count++
19}
20
21func main() {
22	const wantCount = 128
23
24	anotherFunc(wantCount - 1) // -1 because the call to main already counted
25
26	if count == 0 {
27		panic("mayMoreStack not called")
28	} else if count != wantCount {
29		println(count, "!=", wantCount)
30		panic("wrong number of calls to mayMoreStack")
31	}
32}
33
34//go:noinline
35func anotherFunc(n int) {
36	// Trigger a stack growth on at least some calls to
37	// anotherFunc to test that mayMoreStack is called outside the
38	// morestack loop. It's also important that it is called
39	// before (not after) morestack, but that's hard to test.
40	var x [1 << 10]byte
41
42	if n > 1 {
43		anotherFunc(n - 1)
44	}
45
46	runtime.KeepAlive(x)
47}
48