5 // Copyright 2014 The Go Authors. All rights reserved.
6 // Use of this source code is governed by a BSD-style
7 // license that can be found in the LICENSE file.
26 # These are test cases for the linker analysis that detects chains of
27 # nosplit functions that would cause a stack overflow.
29 # Lines beginning with # are comments.
31 # Each test case describes a sequence of functions, one per line.
32 # Each function definition is the function name, then the frame size,
33 # then optionally the keyword 'nosplit', then the body of the function.
34 # The body is assembly code, with some shorthands.
35 # The shorthand 'call x' stands for CALL x(SB).
36 # The shorthand 'callind' stands for 'CALL R0', where R0 is a register.
37 # Each test case must define a function named main, and it must be first.
38 # That is, a line beginning "main " indicates the start of a new test case.
39 # Within a stanza, ; can be used instead of \n to separate lines.
41 # After the function definition, the test case ends with an optional
42 # REJECT line, specifying the architectures on which the case should
43 # be rejected. "REJECT" without any architectures means reject on all architectures.
44 # The linker should accept the test case on systems not explicitly rejected.
46 # 64-bit systems do not attempt to execute test cases with frame sizes
47 # that are only 32-bit aligned.
49 # Ordinary function should work
52 # Large frame marked nosplit is always wrong.
56 # Calling a large frame is okay.
60 # But not if the frame is nosplit.
68 # Recursive nosplit runs out of space.
69 main 0 nosplit call main
72 # Chains of ordinary functions okay.
77 # Chains of nosplit must fit in the stack limit, 128 bytes.
100 f5 16 nosplit call f6
101 f6 16 nosplit call f7
102 f7 16 nosplit call f8
103 f8 16 nosplit call end
107 # Test cases near the 128-byte limit.
109 # Ordinary stack split frame is always okay.
118 # A nosplit leaf can use the whole 128-CallSize bytes available on entry.
123 main 128 nosplit; REJECT
124 main 132 nosplit; REJECT
125 main 136 nosplit; REJECT
127 # Calling a nosplit function from a nosplit function requires
128 # having room for the saved caller PC and the called frame.
129 # Because ARM doesn't save LR in the leaf, it gets an extra 4 bytes.
130 # Because Power64 doesn't save LR in the leaf, it gets an extra 8 bytes.
131 main 112 nosplit call f; f 0 nosplit
132 main 116 nosplit call f; f 0 nosplit
133 main 120 nosplit call f; f 0 nosplit; REJECT amd64
134 main 124 nosplit call f; f 0 nosplit; REJECT amd64 386
135 main 128 nosplit call f; f 0 nosplit; REJECT
136 main 132 nosplit call f; f 0 nosplit; REJECT
137 main 136 nosplit call f; f 0 nosplit; REJECT
139 # Calling a splitting function from a nosplit function requires
140 # having room for the saved caller PC of the call but also the
141 # saved caller PC for the call to morestack.
142 # Again the ARM and Power64 work in less space.
143 main 104 nosplit call f; f 0 call f
144 main 108 nosplit call f; f 0 call f
145 main 112 nosplit call f; f 0 call f; REJECT amd64
146 main 116 nosplit call f; f 0 call f; REJECT amd64
147 main 120 nosplit call f; f 0 call f; REJECT amd64 386
148 main 124 nosplit call f; f 0 call f; REJECT amd64 386
149 main 128 nosplit call f; f 0 call f; REJECT
150 main 132 nosplit call f; f 0 call f; REJECT
151 main 136 nosplit call f; f 0 call f; REJECT
153 # Indirect calls are assumed to be splitting functions.
154 main 104 nosplit callind
155 main 108 nosplit callind
156 main 112 nosplit callind; REJECT amd64
157 main 116 nosplit callind; REJECT amd64
158 main 120 nosplit callind; REJECT amd64 386
159 main 124 nosplit callind; REJECT amd64 386
160 main 128 nosplit callind; REJECT
161 main 132 nosplit callind; REJECT
162 main 136 nosplit callind; REJECT
175 commentRE = regexp.MustCompile(`(?m)^#.*`)
176 rejectRE = regexp.MustCompile(`(?s)\A(.+?)((\n|; *)REJECT(.*))?\z`)
177 lineRE = regexp.MustCompile(`(\w+) (\d+)( nosplit)?(.*)`)
178 callRE = regexp.MustCompile(`\bcall (\w+)\b`)
179 callindRE = regexp.MustCompile(`\bcallind\b`)
183 goarch := os.Getenv("GOARCH")
185 goarch = runtime.GOARCH
188 dir, err := ioutil.TempDir("", "go-test-nosplit")
191 fmt.Printf("creating temp dir: %v\n", err)
194 defer os.RemoveAll(dir)
196 tests = strings.Replace(tests, "\t", " ", -1)
197 tests = commentRE.ReplaceAllString(tests, "")
204 i := strings.Index(tests, "\nmain ")
206 stanza, tests = tests, ""
208 stanza, tests = tests[:i], tests[i+1:]
211 m := rejectRE.FindStringSubmatch(stanza)
214 fmt.Printf("invalid stanza:\n\t%s\n", indent(stanza))
217 lines := strings.TrimSpace(m[1])
220 if strings.TrimSpace(m[4]) == "" {
223 for _, rej := range strings.Fields(m[4]) {
230 if lines == "" && !reject {
234 var gobuf bytes.Buffer
235 fmt.Fprintf(&gobuf, "package main\n")
240 case "power64", "power64le":
242 fmt.Fprintf(&buf, "#define CALL BL\n#define REGISTER (CTR)\n#define RET RETURN\n")
244 fmt.Fprintf(&buf, "#define CALL BL\n#define REGISTER (R0)\n")
247 fmt.Fprintf(&buf, "#define REGISTER AX\n")
249 fmt.Fprintf(&buf, "#define REGISTER AX\n")
252 for _, line := range strings.Split(lines, "\n") {
253 line = strings.TrimSpace(line)
257 for i, subline := range strings.Split(line, ";") {
258 subline = strings.TrimSpace(subline)
262 m := lineRE.FindStringSubmatch(subline)
265 fmt.Printf("invalid function line: %s\n", subline)
269 size, _ := strconv.Atoi(m[2])
271 // The limit was originally 128 but is now 512.
272 // Instead of rewriting the test cases above, adjust
273 // the first stack frame to use up the extra 32 bytes.
278 if size%ptrSize == 4 {
289 body = callRE.ReplaceAllString(body, "CALL ·$1(SB);")
290 body = callindRE.ReplaceAllString(body, "CALL REGISTER;")
292 fmt.Fprintf(&gobuf, "func %s()\n", name)
293 fmt.Fprintf(&buf, "TEXT ·%s(SB)%s,$%d-0\n\t%s\n\tRET\n\n", name, nosplit, size, body)
297 if err := ioutil.WriteFile(filepath.Join(dir, "asm.s"), buf.Bytes(), 0666); err != nil {
300 if err := ioutil.WriteFile(filepath.Join(dir, "main.go"), gobuf.Bytes(), 0666); err != nil {
304 cmd := exec.Command("go", "build")
306 output, err := cmd.CombinedOutput()
311 fmt.Printf("accepted incorrectly:\n\t%s\n", indent(strings.TrimSpace(stanza)))
317 fmt.Printf("rejected incorrectly:\n\t%s\n", indent(strings.TrimSpace(stanza)))
318 fmt.Printf("\n\tlinker output:\n\t%s\n", indent(string(output)))
323 if !bugged && (nok == 0 || nfail == 0) {
325 fmt.Printf("not enough test cases run\n")
329 func indent(s string) string {
330 return strings.Replace(s, "\n", "\n\t", -1)