2020-12-18 01:03:07 +03:00
|
|
|
// +build !nacl,!js,!aix,!gcflags_noopt,gc
|
2015-02-03 21:52:18 +03:00
|
|
|
// run
|
2014-05-20 20:10:19 +04:00
|
|
|
|
2016-04-11 00:32:26 +03:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
2014-04-17 06:08:00 +04:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2014-09-17 01:39:55 +04:00
|
|
|
"log"
|
2014-04-17 06:08:00 +04:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2021-03-18 01:28:38 +03:00
|
|
|
const debug = false
|
|
|
|
|
2014-04-17 06:08:00 +04:00
|
|
|
var tests = `
|
|
|
|
# These are test cases for the linker analysis that detects chains of
|
|
|
|
# nosplit functions that would cause a stack overflow.
|
|
|
|
#
|
|
|
|
# Lines beginning with # are comments.
|
|
|
|
#
|
|
|
|
# Each test case describes a sequence of functions, one per line.
|
|
|
|
# Each function definition is the function name, then the frame size,
|
|
|
|
# then optionally the keyword 'nosplit', then the body of the function.
|
|
|
|
# The body is assembly code, with some shorthands.
|
|
|
|
# The shorthand 'call x' stands for CALL x(SB).
|
|
|
|
# The shorthand 'callind' stands for 'CALL R0', where R0 is a register.
|
2021-03-18 01:28:38 +03:00
|
|
|
# Each test case must define a function named start, and it must be first.
|
|
|
|
# That is, a line beginning "start " indicates the start of a new test case.
|
2014-04-17 06:08:00 +04:00
|
|
|
# Within a stanza, ; can be used instead of \n to separate lines.
|
|
|
|
#
|
|
|
|
# After the function definition, the test case ends with an optional
|
|
|
|
# REJECT line, specifying the architectures on which the case should
|
|
|
|
# be rejected. "REJECT" without any architectures means reject on all architectures.
|
|
|
|
# The linker should accept the test case on systems not explicitly rejected.
|
|
|
|
#
|
|
|
|
# 64-bit systems do not attempt to execute test cases with frame sizes
|
|
|
|
# that are only 32-bit aligned.
|
|
|
|
|
|
|
|
# Ordinary function should work
|
2021-03-18 01:28:38 +03:00
|
|
|
start 0
|
2014-04-17 06:08:00 +04:00
|
|
|
|
|
|
|
# Large frame marked nosplit is always wrong.
|
2021-03-18 01:28:38 +03:00
|
|
|
start 10000 nosplit
|
2014-04-17 06:08:00 +04:00
|
|
|
REJECT
|
|
|
|
|
|
|
|
# Calling a large frame is okay.
|
2021-03-18 01:28:38 +03:00
|
|
|
start 0 call big
|
2014-04-17 06:08:00 +04:00
|
|
|
big 10000
|
|
|
|
|
|
|
|
# But not if the frame is nosplit.
|
2021-03-18 01:28:38 +03:00
|
|
|
start 0 call big
|
2014-04-17 06:08:00 +04:00
|
|
|
big 10000 nosplit
|
|
|
|
REJECT
|
|
|
|
|
|
|
|
# Recursion is okay.
|
2021-03-18 01:28:38 +03:00
|
|
|
start 0 call start
|
2014-04-17 06:08:00 +04:00
|
|
|
|
|
|
|
# Recursive nosplit runs out of space.
|
2021-03-18 01:28:38 +03:00
|
|
|
start 0 nosplit call start
|
2014-04-17 06:08:00 +04:00
|
|
|
REJECT
|
|
|
|
|
|
|
|
# Chains of ordinary functions okay.
|
2021-03-18 01:28:38 +03:00
|
|
|
start 0 call f1
|
2014-04-17 06:08:00 +04:00
|
|
|
f1 80 call f2
|
|
|
|
f2 80
|
|
|
|
|
|
|
|
# Chains of nosplit must fit in the stack limit, 128 bytes.
|
2021-03-18 01:28:38 +03:00
|
|
|
start 0 call f1
|
2014-04-17 06:08:00 +04:00
|
|
|
f1 80 nosplit call f2
|
|
|
|
f2 80 nosplit
|
|
|
|
REJECT
|
|
|
|
|
|
|
|
# Larger chains.
|
2021-03-18 01:28:38 +03:00
|
|
|
start 0 call f1
|
2014-04-17 06:08:00 +04:00
|
|
|
f1 16 call f2
|
|
|
|
f2 16 call f3
|
|
|
|
f3 16 call f4
|
|
|
|
f4 16 call f5
|
|
|
|
f5 16 call f6
|
|
|
|
f6 16 call f7
|
|
|
|
f7 16 call f8
|
|
|
|
f8 16 call end
|
|
|
|
end 1000
|
|
|
|
|
2021-03-18 01:28:38 +03:00
|
|
|
start 0 call f1
|
2014-04-17 06:08:00 +04:00
|
|
|
f1 16 nosplit call f2
|
|
|
|
f2 16 nosplit call f3
|
|
|
|
f3 16 nosplit call f4
|
|
|
|
f4 16 nosplit call f5
|
|
|
|
f5 16 nosplit call f6
|
|
|
|
f6 16 nosplit call f7
|
|
|
|
f7 16 nosplit call f8
|
|
|
|
f8 16 nosplit call end
|
|
|
|
end 1000
|
|
|
|
REJECT
|
|
|
|
|
|
|
|
# Test cases near the 128-byte limit.
|
|
|
|
|
|
|
|
# Ordinary stack split frame is always okay.
|
2021-03-18 01:28:38 +03:00
|
|
|
start 112
|
|
|
|
start 116
|
|
|
|
start 120
|
|
|
|
start 124
|
|
|
|
start 128
|
|
|
|
start 132
|
|
|
|
start 136
|
2014-04-17 06:08:00 +04:00
|
|
|
|
|
|
|
# A nosplit leaf can use the whole 128-CallSize bytes available on entry.
|
2017-10-05 23:14:14 +03:00
|
|
|
# (CallSize is 32 on ppc64, 8 on amd64 for frame pointer.)
|
2021-03-18 01:28:38 +03:00
|
|
|
start 96 nosplit
|
|
|
|
start 100 nosplit; REJECT ppc64 ppc64le
|
|
|
|
start 104 nosplit; REJECT ppc64 ppc64le arm64
|
|
|
|
start 108 nosplit; REJECT ppc64 ppc64le
|
|
|
|
start 112 nosplit; REJECT ppc64 ppc64le arm64
|
|
|
|
start 116 nosplit; REJECT ppc64 ppc64le
|
|
|
|
start 120 nosplit; REJECT ppc64 ppc64le amd64 arm64
|
|
|
|
start 124 nosplit; REJECT ppc64 ppc64le amd64
|
|
|
|
start 128 nosplit; REJECT
|
|
|
|
start 132 nosplit; REJECT
|
|
|
|
start 136 nosplit; REJECT
|
2014-04-17 06:08:00 +04:00
|
|
|
|
|
|
|
# Calling a nosplit function from a nosplit function requires
|
|
|
|
# having room for the saved caller PC and the called frame.
|
|
|
|
# Because ARM doesn't save LR in the leaf, it gets an extra 4 bytes.
|
2015-10-30 02:47:24 +03:00
|
|
|
# Because arm64 doesn't save LR in the leaf, it gets an extra 8 bytes.
|
2017-10-05 23:14:14 +03:00
|
|
|
# ppc64 doesn't save LR in the leaf, but CallSize is 32, so it gets 24 bytes.
|
|
|
|
# Because AMD64 uses frame pointer, it has 8 fewer bytes.
|
2021-03-18 01:28:38 +03:00
|
|
|
start 96 nosplit call f; f 0 nosplit
|
|
|
|
start 100 nosplit call f; f 0 nosplit; REJECT ppc64 ppc64le
|
|
|
|
start 104 nosplit call f; f 0 nosplit; REJECT ppc64 ppc64le arm64
|
|
|
|
start 108 nosplit call f; f 0 nosplit; REJECT ppc64 ppc64le
|
|
|
|
start 112 nosplit call f; f 0 nosplit; REJECT ppc64 ppc64le amd64 arm64
|
|
|
|
start 116 nosplit call f; f 0 nosplit; REJECT ppc64 ppc64le amd64
|
|
|
|
start 120 nosplit call f; f 0 nosplit; REJECT ppc64 ppc64le amd64 arm64
|
|
|
|
start 124 nosplit call f; f 0 nosplit; REJECT ppc64 ppc64le amd64 386
|
|
|
|
start 128 nosplit call f; f 0 nosplit; REJECT
|
|
|
|
start 132 nosplit call f; f 0 nosplit; REJECT
|
|
|
|
start 136 nosplit call f; f 0 nosplit; REJECT
|
2014-04-17 06:08:00 +04:00
|
|
|
|
|
|
|
# Calling a splitting function from a nosplit function requires
|
|
|
|
# having room for the saved caller PC of the call but also the
|
2014-08-14 23:29:37 +04:00
|
|
|
# saved caller PC for the call to morestack.
|
2017-10-05 23:14:14 +03:00
|
|
|
# Architectures differ in the same way as before.
|
2021-03-18 01:28:38 +03:00
|
|
|
start 96 nosplit call f; f 0 call f
|
|
|
|
start 100 nosplit call f; f 0 call f; REJECT ppc64 ppc64le
|
|
|
|
start 104 nosplit call f; f 0 call f; REJECT ppc64 ppc64le amd64 arm64
|
|
|
|
start 108 nosplit call f; f 0 call f; REJECT ppc64 ppc64le amd64
|
|
|
|
start 112 nosplit call f; f 0 call f; REJECT ppc64 ppc64le amd64 arm64
|
|
|
|
start 116 nosplit call f; f 0 call f; REJECT ppc64 ppc64le amd64
|
|
|
|
start 120 nosplit call f; f 0 call f; REJECT ppc64 ppc64le amd64 386 arm64
|
|
|
|
start 124 nosplit call f; f 0 call f; REJECT ppc64 ppc64le amd64 386
|
|
|
|
start 128 nosplit call f; f 0 call f; REJECT
|
|
|
|
start 132 nosplit call f; f 0 call f; REJECT
|
|
|
|
start 136 nosplit call f; f 0 call f; REJECT
|
2014-04-17 06:08:00 +04:00
|
|
|
|
|
|
|
# Indirect calls are assumed to be splitting functions.
|
2021-03-18 01:28:38 +03:00
|
|
|
start 96 nosplit callind
|
|
|
|
start 100 nosplit callind; REJECT ppc64 ppc64le
|
|
|
|
start 104 nosplit callind; REJECT ppc64 ppc64le amd64 arm64
|
|
|
|
start 108 nosplit callind; REJECT ppc64 ppc64le amd64
|
|
|
|
start 112 nosplit callind; REJECT ppc64 ppc64le amd64 arm64
|
|
|
|
start 116 nosplit callind; REJECT ppc64 ppc64le amd64
|
|
|
|
start 120 nosplit callind; REJECT ppc64 ppc64le amd64 386 arm64
|
|
|
|
start 124 nosplit callind; REJECT ppc64 ppc64le amd64 386
|
|
|
|
start 128 nosplit callind; REJECT
|
|
|
|
start 132 nosplit callind; REJECT
|
|
|
|
start 136 nosplit callind; REJECT
|
2014-04-17 06:08:00 +04:00
|
|
|
|
|
|
|
# Issue 7623
|
2021-03-18 01:28:38 +03:00
|
|
|
start 0 call f; f 112
|
|
|
|
start 0 call f; f 116
|
|
|
|
start 0 call f; f 120
|
|
|
|
start 0 call f; f 124
|
|
|
|
start 0 call f; f 128
|
|
|
|
start 0 call f; f 132
|
|
|
|
start 0 call f; f 136
|
2014-04-17 06:08:00 +04:00
|
|
|
`
|
|
|
|
|
|
|
|
var (
|
|
|
|
commentRE = regexp.MustCompile(`(?m)^#.*`)
|
|
|
|
rejectRE = regexp.MustCompile(`(?s)\A(.+?)((\n|; *)REJECT(.*))?\z`)
|
|
|
|
lineRE = regexp.MustCompile(`(\w+) (\d+)( nosplit)?(.*)`)
|
|
|
|
callRE = regexp.MustCompile(`\bcall (\w+)\b`)
|
|
|
|
callindRE = regexp.MustCompile(`\bcallind\b`)
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
goarch := os.Getenv("GOARCH")
|
|
|
|
if goarch == "" {
|
|
|
|
goarch = runtime.GOARCH
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "go-test-nosplit")
|
|
|
|
if err != nil {
|
|
|
|
bug()
|
|
|
|
fmt.Printf("creating temp dir: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
2019-03-12 23:28:03 +03:00
|
|
|
os.Setenv("GOPATH", filepath.Join(dir, "_gopath"))
|
2014-04-17 06:08:00 +04:00
|
|
|
|
2019-02-22 23:58:15 +03:00
|
|
|
if err := ioutil.WriteFile(filepath.Join(dir, "go.mod"), []byte("module go-test-nosplit\n"), 0666); err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
2014-04-17 06:08:00 +04:00
|
|
|
tests = strings.Replace(tests, "\t", " ", -1)
|
|
|
|
tests = commentRE.ReplaceAllString(tests, "")
|
|
|
|
|
|
|
|
nok := 0
|
|
|
|
nfail := 0
|
|
|
|
TestCases:
|
|
|
|
for len(tests) > 0 {
|
|
|
|
var stanza string
|
2021-03-18 01:28:38 +03:00
|
|
|
i := strings.Index(tests, "\nstart ")
|
2014-04-17 06:08:00 +04:00
|
|
|
if i < 0 {
|
|
|
|
stanza, tests = tests, ""
|
|
|
|
} else {
|
|
|
|
stanza, tests = tests[:i], tests[i+1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
m := rejectRE.FindStringSubmatch(stanza)
|
|
|
|
if m == nil {
|
|
|
|
bug()
|
|
|
|
fmt.Printf("invalid stanza:\n\t%s\n", indent(stanza))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
lines := strings.TrimSpace(m[1])
|
|
|
|
reject := false
|
|
|
|
if m[2] != "" {
|
|
|
|
if strings.TrimSpace(m[4]) == "" {
|
|
|
|
reject = true
|
|
|
|
} else {
|
|
|
|
for _, rej := range strings.Fields(m[4]) {
|
|
|
|
if rej == goarch {
|
|
|
|
reject = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if lines == "" && !reject {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-09-17 01:39:55 +04:00
|
|
|
var gobuf bytes.Buffer
|
|
|
|
fmt.Fprintf(&gobuf, "package main\n")
|
|
|
|
|
2014-04-17 06:08:00 +04:00
|
|
|
var buf bytes.Buffer
|
2014-08-14 21:59:58 +04:00
|
|
|
ptrSize := 4
|
|
|
|
switch goarch {
|
2016-12-15 20:05:26 +03:00
|
|
|
case "mips", "mipsle":
|
2017-10-05 23:14:14 +03:00
|
|
|
fmt.Fprintf(&buf, "#define REGISTER (R0)\n")
|
2015-09-29 00:30:32 +03:00
|
|
|
case "mips64", "mips64le":
|
|
|
|
ptrSize = 8
|
2017-10-05 23:14:14 +03:00
|
|
|
fmt.Fprintf(&buf, "#define REGISTER (R0)\n")
|
2014-12-06 03:13:20 +03:00
|
|
|
case "ppc64", "ppc64le":
|
2014-08-14 21:59:58 +04:00
|
|
|
ptrSize = 8
|
2017-10-05 23:14:14 +03:00
|
|
|
fmt.Fprintf(&buf, "#define REGISTER (CTR)\n")
|
2014-08-14 21:59:58 +04:00
|
|
|
case "arm":
|
2017-10-05 23:14:14 +03:00
|
|
|
fmt.Fprintf(&buf, "#define REGISTER (R0)\n")
|
2015-03-08 16:27:51 +03:00
|
|
|
case "arm64":
|
|
|
|
ptrSize = 8
|
2017-10-05 23:14:14 +03:00
|
|
|
fmt.Fprintf(&buf, "#define REGISTER (R0)\n")
|
2014-08-14 21:59:58 +04:00
|
|
|
case "amd64":
|
|
|
|
ptrSize = 8
|
|
|
|
fmt.Fprintf(&buf, "#define REGISTER AX\n")
|
2020-01-24 19:10:04 +03:00
|
|
|
case "riscv64":
|
|
|
|
ptrSize = 8
|
|
|
|
fmt.Fprintf(&buf, "#define REGISTER A0\n")
|
2016-03-21 02:41:34 +03:00
|
|
|
case "s390x":
|
|
|
|
ptrSize = 8
|
|
|
|
fmt.Fprintf(&buf, "#define REGISTER R10\n")
|
2014-08-14 21:59:58 +04:00
|
|
|
default:
|
2014-04-17 06:08:00 +04:00
|
|
|
fmt.Fprintf(&buf, "#define REGISTER AX\n")
|
|
|
|
}
|
|
|
|
|
2021-03-18 01:28:38 +03:00
|
|
|
// Since all of the functions we're generating are
|
|
|
|
// ABI0, first enter ABI0 via a splittable function
|
|
|
|
// and then go to the chain we're testing. This way we
|
|
|
|
// don't have to account for ABI wrappers in the chain.
|
|
|
|
fmt.Fprintf(&gobuf, "func main0()\n")
|
|
|
|
fmt.Fprintf(&gobuf, "func main() { main0() }\n")
|
|
|
|
fmt.Fprintf(&buf, "TEXT ·main0(SB),0,$0-0\n\tCALL ·start(SB)\n")
|
|
|
|
|
2014-04-17 06:08:00 +04:00
|
|
|
for _, line := range strings.Split(lines, "\n") {
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
if line == "" {
|
|
|
|
continue
|
|
|
|
}
|
2014-08-27 22:08:26 +04:00
|
|
|
for i, subline := range strings.Split(line, ";") {
|
2014-04-17 06:08:00 +04:00
|
|
|
subline = strings.TrimSpace(subline)
|
|
|
|
if subline == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m := lineRE.FindStringSubmatch(subline)
|
|
|
|
if m == nil {
|
|
|
|
bug()
|
|
|
|
fmt.Printf("invalid function line: %s\n", subline)
|
|
|
|
continue TestCases
|
|
|
|
}
|
|
|
|
name := m[1]
|
|
|
|
size, _ := strconv.Atoi(m[2])
|
2014-08-27 22:08:26 +04:00
|
|
|
|
runtime: static lock ranking for the runtime (enabled by GOEXPERIMENT)
I took some of the infrastructure from Austin's lock logging CR
https://go-review.googlesource.com/c/go/+/192704 (with deadlock
detection from the logs), and developed a setup to give static lock
ranking for runtime locks.
Static lock ranking establishes a documented total ordering among locks,
and then reports an error if the total order is violated. This can
happen if a deadlock happens (by acquiring a sequence of locks in
different orders), or if just one side of a possible deadlock happens.
Lock ordering deadlocks cannot happen as long as the lock ordering is
followed.
Along the way, I found a deadlock involving the new timer code, which Ian fixed
via https://go-review.googlesource.com/c/go/+/207348, as well as two other
potential deadlocks.
See the constants at the top of runtime/lockrank.go to show the static
lock ranking that I ended up with, along with some comments. This is
great documentation of the current intended lock ordering when acquiring
multiple locks in the runtime.
I also added an array lockPartialOrder[] which shows and enforces the
current partial ordering among locks (which is embedded within the total
ordering). This is more specific about the dependencies among locks.
I don't try to check the ranking within a lock class with multiple locks
that can be acquired at the same time (i.e. check the ranking when
multiple hchan locks are acquired).
Currently, I am doing a lockInit() call to set the lock rank of most
locks. Any lock that is not otherwise initialized is assumed to be a
leaf lock (a very high rank lock), so that eliminates the need to do
anything for a bunch of locks (including all architecture-dependent
locks). For two locks, root.lock and notifyList.lock (only in the
runtime/sema.go file), it is not as easy to do lock initialization, so
instead, I am passing the lock rank with the lock calls.
For Windows compilation, I needed to increase the StackGuard size from
896 to 928 because of the new lock-rank checking functions.
Checking of the static lock ranking is enabled by setting
GOEXPERIMENT=staticlockranking before doing a run.
To make sure that the static lock ranking code has no overhead in memory
or CPU when not enabled by GOEXPERIMENT, I changed 'go build/install' so
that it defines a build tag (with the same name) whenever any experiment
has been baked into the toolchain (by checking Expstring()). This allows
me to avoid increasing the size of the 'mutex' type when static lock
ranking is not enabled.
Fixes #38029
Change-Id: I154217ff307c47051f8dae9c2a03b53081acd83a
Reviewed-on: https://go-review.googlesource.com/c/go/+/207619
Reviewed-by: Dan Scales <danscales@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-11-14 04:34:47 +03:00
|
|
|
// The limit was originally 128 but is now 800 (928-128).
|
2014-08-27 22:08:26 +04:00
|
|
|
// Instead of rewriting the test cases above, adjust
|
2015-05-01 19:36:18 +03:00
|
|
|
// the first stack frame to use up the extra bytes.
|
2014-08-27 22:08:26 +04:00
|
|
|
if i == 0 {
|
runtime: static lock ranking for the runtime (enabled by GOEXPERIMENT)
I took some of the infrastructure from Austin's lock logging CR
https://go-review.googlesource.com/c/go/+/192704 (with deadlock
detection from the logs), and developed a setup to give static lock
ranking for runtime locks.
Static lock ranking establishes a documented total ordering among locks,
and then reports an error if the total order is violated. This can
happen if a deadlock happens (by acquiring a sequence of locks in
different orders), or if just one side of a possible deadlock happens.
Lock ordering deadlocks cannot happen as long as the lock ordering is
followed.
Along the way, I found a deadlock involving the new timer code, which Ian fixed
via https://go-review.googlesource.com/c/go/+/207348, as well as two other
potential deadlocks.
See the constants at the top of runtime/lockrank.go to show the static
lock ranking that I ended up with, along with some comments. This is
great documentation of the current intended lock ordering when acquiring
multiple locks in the runtime.
I also added an array lockPartialOrder[] which shows and enforces the
current partial ordering among locks (which is embedded within the total
ordering). This is more specific about the dependencies among locks.
I don't try to check the ranking within a lock class with multiple locks
that can be acquired at the same time (i.e. check the ranking when
multiple hchan locks are acquired).
Currently, I am doing a lockInit() call to set the lock rank of most
locks. Any lock that is not otherwise initialized is assumed to be a
leaf lock (a very high rank lock), so that eliminates the need to do
anything for a bunch of locks (including all architecture-dependent
locks). For two locks, root.lock and notifyList.lock (only in the
runtime/sema.go file), it is not as easy to do lock initialization, so
instead, I am passing the lock rank with the lock calls.
For Windows compilation, I needed to increase the StackGuard size from
896 to 928 because of the new lock-rank checking functions.
Checking of the static lock ranking is enabled by setting
GOEXPERIMENT=staticlockranking before doing a run.
To make sure that the static lock ranking code has no overhead in memory
or CPU when not enabled by GOEXPERIMENT, I changed 'go build/install' so
that it defines a build tag (with the same name) whenever any experiment
has been baked into the toolchain (by checking Expstring()). This allows
me to avoid increasing the size of the 'mutex' type when static lock
ranking is not enabled.
Fixes #38029
Change-Id: I154217ff307c47051f8dae9c2a03b53081acd83a
Reviewed-on: https://go-review.googlesource.com/c/go/+/207619
Reviewed-by: Dan Scales <danscales@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-11-14 04:34:47 +03:00
|
|
|
size += (928 - 128) - 128
|
2015-05-01 19:36:18 +03:00
|
|
|
// Noopt builds have a larger stackguard.
|
2015-10-19 18:36:07 +03:00
|
|
|
// See ../src/cmd/dist/buildruntime.go:stackGuardMultiplier
|
2017-04-18 22:53:25 +03:00
|
|
|
// This increase is included in objabi.StackGuard
|
2015-05-01 19:36:18 +03:00
|
|
|
for _, s := range strings.Split(os.Getenv("GO_GCFLAGS"), " ") {
|
|
|
|
if s == "-N" {
|
runtime: static lock ranking for the runtime (enabled by GOEXPERIMENT)
I took some of the infrastructure from Austin's lock logging CR
https://go-review.googlesource.com/c/go/+/192704 (with deadlock
detection from the logs), and developed a setup to give static lock
ranking for runtime locks.
Static lock ranking establishes a documented total ordering among locks,
and then reports an error if the total order is violated. This can
happen if a deadlock happens (by acquiring a sequence of locks in
different orders), or if just one side of a possible deadlock happens.
Lock ordering deadlocks cannot happen as long as the lock ordering is
followed.
Along the way, I found a deadlock involving the new timer code, which Ian fixed
via https://go-review.googlesource.com/c/go/+/207348, as well as two other
potential deadlocks.
See the constants at the top of runtime/lockrank.go to show the static
lock ranking that I ended up with, along with some comments. This is
great documentation of the current intended lock ordering when acquiring
multiple locks in the runtime.
I also added an array lockPartialOrder[] which shows and enforces the
current partial ordering among locks (which is embedded within the total
ordering). This is more specific about the dependencies among locks.
I don't try to check the ranking within a lock class with multiple locks
that can be acquired at the same time (i.e. check the ranking when
multiple hchan locks are acquired).
Currently, I am doing a lockInit() call to set the lock rank of most
locks. Any lock that is not otherwise initialized is assumed to be a
leaf lock (a very high rank lock), so that eliminates the need to do
anything for a bunch of locks (including all architecture-dependent
locks). For two locks, root.lock and notifyList.lock (only in the
runtime/sema.go file), it is not as easy to do lock initialization, so
instead, I am passing the lock rank with the lock calls.
For Windows compilation, I needed to increase the StackGuard size from
896 to 928 because of the new lock-rank checking functions.
Checking of the static lock ranking is enabled by setting
GOEXPERIMENT=staticlockranking before doing a run.
To make sure that the static lock ranking code has no overhead in memory
or CPU when not enabled by GOEXPERIMENT, I changed 'go build/install' so
that it defines a build tag (with the same name) whenever any experiment
has been baked into the toolchain (by checking Expstring()). This allows
me to avoid increasing the size of the 'mutex' type when static lock
ranking is not enabled.
Fixes #38029
Change-Id: I154217ff307c47051f8dae9c2a03b53081acd83a
Reviewed-on: https://go-review.googlesource.com/c/go/+/207619
Reviewed-by: Dan Scales <danscales@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-11-14 04:34:47 +03:00
|
|
|
size += 928
|
2015-05-01 19:36:18 +03:00
|
|
|
}
|
|
|
|
}
|
2014-08-27 22:08:26 +04:00
|
|
|
}
|
|
|
|
|
2018-08-29 09:55:03 +03:00
|
|
|
if size%ptrSize == 4 {
|
2014-04-17 06:08:00 +04:00
|
|
|
continue TestCases
|
|
|
|
}
|
|
|
|
nosplit := m[3]
|
|
|
|
body := m[4]
|
|
|
|
|
|
|
|
if nosplit != "" {
|
|
|
|
nosplit = ",7"
|
|
|
|
} else {
|
|
|
|
nosplit = ",0"
|
|
|
|
}
|
|
|
|
body = callRE.ReplaceAllString(body, "CALL ·$1(SB);")
|
|
|
|
body = callindRE.ReplaceAllString(body, "CALL REGISTER;")
|
|
|
|
|
2014-09-17 01:39:55 +04:00
|
|
|
fmt.Fprintf(&gobuf, "func %s()\n", name)
|
2014-04-17 06:08:00 +04:00
|
|
|
fmt.Fprintf(&buf, "TEXT ·%s(SB)%s,$%d-0\n\t%s\n\tRET\n\n", name, nosplit, size, body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 01:28:38 +03:00
|
|
|
if debug {
|
|
|
|
fmt.Printf("===\n%s\n", strings.TrimSpace(stanza))
|
|
|
|
fmt.Printf("-- main.go --\n%s", gobuf.String())
|
|
|
|
fmt.Printf("-- asm.s --\n%s", buf.String())
|
|
|
|
}
|
|
|
|
|
2014-09-17 01:39:55 +04:00
|
|
|
if err := ioutil.WriteFile(filepath.Join(dir, "asm.s"), buf.Bytes(), 0666); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(dir, "main.go"), gobuf.Bytes(), 0666); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-04-17 06:08:00 +04:00
|
|
|
|
2021-03-18 01:28:38 +03:00
|
|
|
cmd := exec.Command("go", "build")
|
2014-04-17 06:08:00 +04:00
|
|
|
cmd.Dir = dir
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err == nil {
|
|
|
|
nok++
|
|
|
|
if reject {
|
|
|
|
bug()
|
|
|
|
fmt.Printf("accepted incorrectly:\n\t%s\n", indent(strings.TrimSpace(stanza)))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nfail++
|
|
|
|
if !reject {
|
|
|
|
bug()
|
|
|
|
fmt.Printf("rejected incorrectly:\n\t%s\n", indent(strings.TrimSpace(stanza)))
|
|
|
|
fmt.Printf("\n\tlinker output:\n\t%s\n", indent(string(output)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bugged && (nok == 0 || nfail == 0) {
|
|
|
|
bug()
|
|
|
|
fmt.Printf("not enough test cases run\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func indent(s string) string {
|
|
|
|
return strings.Replace(s, "\n", "\n\t", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
var bugged = false
|
|
|
|
|
|
|
|
func bug() {
|
|
|
|
if !bugged {
|
|
|
|
bugged = true
|
|
|
|
fmt.Printf("BUG\n")
|
|
|
|
}
|
|
|
|
}
|